如何确定哪个鼠标按钮引发了WPF中的click事件?

时间:2021-07-21 23:15:20

I have a button that I trigger OnClick whenever there is a click on that button. I would like to know which Mouse button clicked on that button?

我有一个按钮,只要点击该按钮,我就会触发OnClick。我想知道哪个鼠标按钮点击了该按钮?

When I use the Mouse.LeftButton or Mouse.RightButton, both tell me "realsed" which is their states after the click.

当我使用Mouse.LeftButton或Mouse.RightButton时,两者都告诉我“实现”,这是他们点击后的状态。

I just want to know which one clicked on my button. If I change EventArgs to MouseEventArgs, I receive errors.

我只是想知道哪一个点击我的按钮。如果我将EventArgs更改为MouseEventArgs,则会收到错误。

XAML: <Button Name="myButton" Click="OnClick">

XAML:

private void OnClick(object sender, EventArgs e)
{
//do certain thing. 
}

3 个解决方案

#1


If you're just using the Button's Click event, then the only mouse button that will fire it is the primary mouse button.

如果您只是使用Button的Click事件,那么将触发它的唯一鼠标按钮是主鼠标按钮。

If you still need to know specifically whether it was the left or right button, then you can use the SystemInformation to obtain it.

如果您仍需要具体了解它是左按钮还是右按钮,则可以使用SystemInformation来获取它。

void OnClick(object sender, RoutedEventArgs e)
    {
        if (SystemParameters.SwapButtons) // Or use SystemInformation.MouseButtonsSwapped
        {
            // It's the right button.
        }
        else
        {
            // It's the standard left button.
        }
    }

Edit: The WPF equivalent to SystemInformation is SystemParameters, which can be used instead. Though you can include System.Windows.Forms as a reference to obtain the SystemInformation without adversely effecting the application in any way.

编辑:与SystemInformation等效的WPF是SystemParameters,可以替代使用它。虽然您可以包含System.Windows.Forms作为参考来获取SystemInformation,而不会以任何方式对应用程序产生负面影响。

#2


You can cast like below:

您可以如下投射:

MouseEventArgs myArgs = (MouseEventArgs) e;

And then get the information with:

然后通过以下方式获取信息:

if (myArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
    // do sth
}

The solution works in VS2013 and you do not have to use MouseClick event anymore ;)

该解决方案适用于VS2013,您不必再使用MouseClick事件;)

#3


You're right, Jose, it's with MouseClick event. But you must add a little delegate:

你是对的,何塞,这是与MouseClick事件。但是你必须添加一个小代表:

this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);

this.button1.MouseDown + = new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);

And use this method in your form:

并在您的表单中使用此方法:

    private void MyMouseDouwn(object sender, MouseEventArgs e) 
    {
        if (e.Button == MouseButtons.Right)
           this.Text = "Right";

        if (e.Button == MouseButtons.Left)
            this.Text = "Left";
    }

#1


If you're just using the Button's Click event, then the only mouse button that will fire it is the primary mouse button.

如果您只是使用Button的Click事件,那么将触发它的唯一鼠标按钮是主鼠标按钮。

If you still need to know specifically whether it was the left or right button, then you can use the SystemInformation to obtain it.

如果您仍需要具体了解它是左按钮还是右按钮,则可以使用SystemInformation来获取它。

void OnClick(object sender, RoutedEventArgs e)
    {
        if (SystemParameters.SwapButtons) // Or use SystemInformation.MouseButtonsSwapped
        {
            // It's the right button.
        }
        else
        {
            // It's the standard left button.
        }
    }

Edit: The WPF equivalent to SystemInformation is SystemParameters, which can be used instead. Though you can include System.Windows.Forms as a reference to obtain the SystemInformation without adversely effecting the application in any way.

编辑:与SystemInformation等效的WPF是SystemParameters,可以替代使用它。虽然您可以包含System.Windows.Forms作为参考来获取SystemInformation,而不会以任何方式对应用程序产生负面影响。

#2


You can cast like below:

您可以如下投射:

MouseEventArgs myArgs = (MouseEventArgs) e;

And then get the information with:

然后通过以下方式获取信息:

if (myArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
    // do sth
}

The solution works in VS2013 and you do not have to use MouseClick event anymore ;)

该解决方案适用于VS2013,您不必再使用MouseClick事件;)

#3


You're right, Jose, it's with MouseClick event. But you must add a little delegate:

你是对的,何塞,这是与MouseClick事件。但是你必须添加一个小代表:

this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);

this.button1.MouseDown + = new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);

And use this method in your form:

并在您的表单中使用此方法:

    private void MyMouseDouwn(object sender, MouseEventArgs e) 
    {
        if (e.Button == MouseButtons.Right)
           this.Text = "Right";

        if (e.Button == MouseButtons.Left)
            this.Text = "Left";
    }