C# 如何确定哪个鼠标按钮在 WPF 中引发了单击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/977329/
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
How can I determine which mouse button raised the click event in WPF?
提问by paradisontheitroad
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
or 时Mouse.RightButton
,两者都告诉我“ realsed”,这是它们在点击后的状态。
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:<Button Name="myButton" Click="OnClick">
private void OnClick(object sender, EventArgs e)
{
//do certain thing.
}
采纳答案by rmoore
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 的参考,而不会以任何方式对应用程序产生不利影响。
回答by Broken_Window
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";
}
回答by Sebastian Xawery Wi?niowiecki
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 事件;)