我有一个按钮,OnClick只要单击该按钮即可触发。我想知道哪个鼠标按钮单击了该按钮?
当我使用Mouse.LeftButton或时Mouse.RightButton,两者都告诉我“ 实现 ”,即单击后它们的状态。
我只想知道哪个人点击了我的按钮。如果更改EventArgs为MouseEventArgs,则会收到错误消息。
XAML: <Button Name="myButton" Click="OnClick">
private void OnClick(object sender, EventArgs e)
{
//do certain thing.
}
Run Code Online (Sandbox Code Playgroud)
您可以像下面这样投射:
MouseEventArgs myArgs = (MouseEventArgs) e;
Run Code Online (Sandbox Code Playgroud)
然后通过以下方式获取信息:
if (myArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
// do sth
}
Run Code Online (Sandbox Code Playgroud)
该解决方案可在VS2013中使用,您不必再使用MouseClick事件;)
如果您只是使用按钮的 Click 事件,那么唯一会触发该事件的鼠标按钮就是主鼠标按钮。
如果你还需要具体知道是左键还是右键,那么你可以使用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.
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:与 SystemInformation 等效的 WPF 是 SystemParameters,可以使用它来代替。尽管您可以包含 System.Windows.Forms 作为引用来获取 SystemInformation,而不会以任何方式对应用程序产生不利影响。
| 归档时间: |
|
| 查看次数: |
13918 次 |
| 最近记录: |