如何在 wpf C# 中检查鼠标按钮是向左还是向右?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41718317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:59:28  来源:igfitidea点击:

How can I check if mouse button is left or right in wpf C#?

c#wpfwpf-controlsmouse

提问by Shahbaz

I'm trying this code actually I have created only one Eventhandler that is on Click="button_Click".

我正在尝试这段代码,实际上我只创建了一个位于Click="button_Click".

XAML:

XAML:

<Window x:Class="WPFAPP.Window1" 
    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:WPFAPP"
    mc:Ignorable="d"
    Title="Window1" Height="447.625" Width="562">

    <Grid>
        <Button x:Name="btn1" Content="Button1" Click="button_Click" HorizontalAlignment="Left" Margin="26,22,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn2" Content="Button2" Click="button_Click" HorizontalAlignment="Left" Margin="26,61,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn3" Content="Button3" Click="button_Click" HorizontalAlignment="Left" Margin="26,100,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn4" Content="Button4" Click="button_Click" HorizontalAlignment="Left" Margin="26,137,0,0" VerticalAlignment="Top" Width="75"/>
        <Button x:Name="btn5" Content="Button5" Click="button_Click" HorizontalAlignment="Left" Margin="26,174,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window> 

Code Behind C#:

C# 背后的代码:

private void button_Click(object sender, RoutedEventArgs e)
{
    Button button = (Button)sender;
    if(e.Equals(Mouse.RightButton))
    {
        button.ClearValue(Button.BackgroundProperty);
        button.Background = Brushes.Green;
    } 
} 

回答by MikeT

Click is only designed for the most limited interaction, if you use the more advanced mouse events you then get a MouseButtonEventArgs which gives you all details about the event.

Click 仅用于最有限的交互,如果您使用更高级的鼠标事件,您将获得一个 MouseButtonEventArgs,它为您提供有关该事件的所有详细信息。

the reason for this is that Click isn't a mouse event, you could also trigger it with a touch, stylus, you can even trigger it with the keyboard by pressing Return while highlighted

这样做的原因是 Click 不是鼠标事件,您也可以通过触摸、手写笔触发它,您甚至可以通过在突出显示时按 Return 来使用键盘触发它

so try MouseDown, MouseUp or DoubleClick instead

所以试试 MouseDown、MouseUp 或 DoubleClick

eg

例如

<Button MouseDoubleClick="Button_MouseDoubleClick" >Click me</Button>
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if(e.ChangedButton == MouseButton.Right)
    {
    }
    e.Handled = true;
}

or for mouse down

或鼠标按下

<Button MouseDown="Button_MouseDown" >Click me</Button>
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
    if(e.ChangedButton == MouseButton.Right)
    {
    }
    e.Handled = true;
}

回答by Rudra

It's always better to have separate event handler for left and right mouse down. If you need to work as per the clicked button. Below is my approach.

为左右鼠标按下单独的事件处理程序总是更好。如果您需要按照点击的按钮工作。下面是我的做法。

<Button MouseLeftButtonDown="Button_MouseLeftButtonDown" MouseRightButtonDown="Button_MouseRightButtonDown"></Button>

Below is code behind.

下面是代码。

private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

}

private void Button_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{

}

If you want to get the button which cause the event, you can check the e.OriginalSourceproperty.

如果您想获取导致事件的按钮,您可以检查该e.OriginalSource属性。