我有一个我触发的按钮 OnClick 每当有单击该按钮时。我想知道哪个鼠标按钮单击该按钮?

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

我只想知道我的按钮点击了哪一个。如果我改变 EventArgsMouseEventArgs, ,我收到错误。

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

private void OnClick(object sender, EventArgs e)
{
//do certain thing. 
}
有帮助吗?

解决方案

如果您只是使用按钮的单击事件,则唯一会触发的鼠标按钮是主鼠标按钮。

如果您仍然需要明确知道它是左还是右键,则可以使用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.
        }
    }

编辑: 等效于系统信息的WPF是SystemParameters,可以使用。虽然您可以将System.Windows.Forms作为参考,以获取系统信息,而不会以任何方式对应用程序产生不利影响。

其他提示

您可以像下面的那样投射:

MouseEventArgs myArgs = (MouseEventArgs) e;

然后获取信息:

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

该解决方案在VS2013中起作用,您不必再使用MouseClick事件了;)

您是对的,何塞,这是Mouseclick活动。但是您必须添加一点委托:

this.button1.mousedown += new System.windows.forms.mouseeventhandler(this.mymousedouwn);

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

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

        if (e.Button == MouseButtons.Left)
            this.Text = "Left";
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top