简体   繁体   English

仅当用户ctrl单击按钮的下半部分时才如何触发事件?

[英]How to trigger an event only when the user ctrl click the lower half of a button?

I want to implement some secret features in my application and they can only be invoked by ctrl left click the lower half of a button. 我想在我的应用程序中实现一些秘密功能,并且只能通过ctrl左键单击按钮的下半部分来调用它们。 Is this possible to implement in WPF? 这可以在WPF中实现吗? I tried to create a demo app and debugging doesn't show me the cursor position information in the click event handler. 我尝试创建一个演示应用程序,并且调试未在click事件处理程序中向我显示光标位置信息。 Below is my test code. 下面是我的测试代码。 Any ideas? 有任何想法吗?

<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

        }
    }
}

You can use the MouseUp or MouseDown events, which both give you the mouse position in the event arguments, as well as which mouse button was pressed. 您可以使用MouseUp或MouseDown事件,这两个事件都可以为您提供事件参数中的鼠标位置以及按下的鼠标按钮。

Mouse up is triggered when the mouse button is lifted while inside the button, while mouse down (can you guess?) when it's pressed down while inside the button. 当在按钮内部将鼠标按钮抬起时,将触发鼠标上移;在按钮内部被按下时,按下鼠标将触发(您能猜到吗?)。

EDIT : I just checked the details for MouseDown and you'll have to use 编辑:我只是检查了MouseDown的详细信息,您将不得不使用

Point p = e.GetPosition(yourButton);

to get the position of the mouse relative to the button (you can replace yourButton by any control to get the mouse position relative to it) 获取鼠标相对于按钮的位置(您可以用任何控件替换yourButton以获得鼠标相对于按钮的位置)

There are so many ways to do this! 有很多方法可以做到这一点!

In addition to the answers provided, which I think are correct, you can also play around with the layout. 除了提供的答案(我认为是正确的)之外,您还可以尝试布局。 For example, you can define a border, containing two buttons where the user will think that there's only one button: 例如,您可以定义一个边框,其中包含两个按钮,用户会认为其中只有一个按钮:

<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>

By removing borders from the buttons in a style that you defined, you would have something like this: 通过以您定义的样式从按钮上删除边框,您将获得以下内容:

在此处输入图片说明

You can also set the background color of the border when the user hovers its mouse on it, using MouseEnter and MouseLeave events. 您还可以使用MouseEnter和MouseLeave事件,在用户将鼠标悬停在边框上时设置边框的背景颜色。

Anyway, when the user clicks the secret area button, you can simply handle the event: 无论如何,当用户单击秘密区域按钮时,您可以简单地处理事件:

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

You can use Mouse.GetPosition to get the cursor position relative to the corner of the button and compare the position to the ActualHeight of the button: 您可以使用Mouse.GetPosition来获取相对于按钮角的光标位置,并将该位置与按钮的ActualHeight进行比较:

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

Add a second button on top of the lower half of your existing button, but set it to be invisible to the user. 在现有按钮的下半部分上方添加第二个按钮,但将其设置为对用户不可见。

You can then program its click handler to do exactly what you want, and even activate the click handler of the underlying, visible button if you so desire. 然后,您可以对其点击处理程序进行编程,以完全按照您的要求进行操作,甚至可以根据需要激活底层可见按钮的点击处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF用户控件中触发单击事件(带按钮的文本框)? - How to Trigger Click Event in WPF User control (Textbox with Button)? 用户单击列表视图的项目时如何触发事件? - How to trigger an event when user click on the item of the list view? 在aspx中如何在源cs文件中定义的按钮单击事件时按下文本框上的“Enter”时触发按钮单击事件 - In aspx how to trigger button click event when press “Enter” on a textbox while the button click event defined in source cs file 如果按钮使用javascript或jQuery同时具有客户端和服务器端单击事件,如何仅触发服务器端单击事件? - How to trigger only server side click event if button have both client and server side click event using javascript or jQuery? 当我单击按钮ajax fonksyonu时,我触发了,但未触发按钮的单击事件 - When I click the button ajax fonksyonu my trigger but does not trigger my click event to button 如何在c#中按Enter键触发按钮点击事件 - How to trigger button click event on pressing enter button in c# 单击动态添加按钮时,在代码后面触发事件 - Trigger Event in behind code when click on Dynamically added button 如何在没有名称的按钮上触发点击事件 - How can i trigger a click event at a Button which has no name 如何通过单击按钮触发 form_Load 事件 - How to trigger form_Load event with button click 如何每5秒自动触发一次Button_Click事件? - How to Auto Trigger Button_Click event every 5 sec?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM