【问题标题】:How to trigger an event only when the user ctrl click the lower half of a button?仅当用户 ctrl 单击按钮的下半部分时如何触发事件?
【发布时间】:2016-12-18 14:05:27
【问题描述】:

我要实现我的应用程序的一些秘密功能和它们只能按ctrl调用左边点击一个按钮的下半部分。这是可能的WPF来实现?我试图创建一个演示应用程序调试不显示我的单击事件处理程序中的光标位置信息。下面是我的测试代码。有什么想法吗?

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="255.284" Width="313.918">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="81,89,0,0" VerticalAlignment="Top" Width="158" Height="49" Click="button_Click"/>
    </Grid>
</Window>
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        { // <----- set break point here, can't find cursor info in e and sender

        }
    }
}

【问题讨论】:

    标签: c# wpf events event-handling


    【解决方案1】:

    您可以使用Mouse.GetPosition获取光标相对于按钮角的位置,并将该位置与按钮的ActualHeight进行比较:

    var pos = Mouse.GetPosition(button);
    if(pos.Y >= button.ActualHeight * 0.5)
    {
        //do something
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 MouseUp 或 MouseDown 事件,它们都为您提供事件参数中的鼠标位置,以及按下了哪个鼠标按钮。

      当鼠标按钮在按钮内被抬起时触发鼠标上移,而当鼠标在按钮内被按下时(你能猜到吗?)。

      编辑:我刚刚检查了 MouseDown 的详细信息,您必须使用

      Point p = e.GetPosition(yourButton);
      

      获取鼠标相对于按钮的位置(您可以将yourButton替换为任何控件以获取鼠标相对于它的位置)

      【讨论】:

        【解决方案3】:

        在现有按钮的下半部分顶部添加第二个按钮,但将其设置为对用户不可见。

        然后,您可以对其点击处理程序进行编程,使其完全按照您的意愿行事,甚至可以根据需要激活底层可见按钮的点击处理程序。

        【讨论】:

        • 为什么不呢?当单击 GUI 上的某个区域时,OP 需要特殊功能。这听起来像是一个按钮的工作。
        • 我可能会工作,但如果你可以在不添加新的(隐藏的)UI 元素的情况下做到这一点,对我来说它更干净
        【解决方案4】:

        有很多方法可以做到这一点!

        除了我认为正确的答案之外,您还可以尝试布局。例如,您可以定义一个边框,其中包含两个按钮,用户会认为只有一个按钮:

        <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="250" Height="100" >
            <Border BorderBrush="Black" BorderThickness="1" MouseEnter="border_MouseEnter" MouseLeave="border_MouseLeave">
                <StackPanel>
                    <Button  VerticalAlignment="Top" Height="50" Style="{StaticResource ButtonStyle}" Content="MainButton"></Button>
                    <Button  VerticalAlignment="Bottom" Content="SecretArea" Style="{StaticResource ButtonStyle}" Height="50" Click="Button_Click"></Button>
                </StackPanel>
            </Border>
        </Grid>
        

        通过从您定义的样式中移除按钮的边框,您将拥有如下效果:

        当用户将鼠标悬停在边框上时,您还可以使用 MouseEnter 和 MouseLeave 事件设置边框的背景颜色。

        无论如何,当用户点击秘密区域按钮时,您可以简单地处理事件:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
              MessageBox.Show("Secret Area");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-30
          • 1970-01-01
          • 2017-10-14
          • 2011-04-29
          • 2014-09-21
          • 2019-07-05
          • 1970-01-01
          相关资源
          最近更新 更多