检测按钮是向右还是左击

我的问题是因为我认为很简单,但它变得有点复杂,我想要实现的是:我在WinForm中有几个按钮,几乎完全相同,所以我创建了一个单独的事件来处理所有这些,现在我希望根据他们点击的鼠标按钮给他们一些其他function,例如,如果按下左键单击按钮执行,但如果右键单击以其他方式执行,可以要实现?,任何帮助将不胜感激。

我正在寻找像这样的声明……

private void buttons_Click(object sender, EventArgs e) { if(e.buttons==mousebuttons.right) //do something else //do other thing } 

你知道的事情是,这是无法实现的,因为e上没有mouse button events ,因为我被称为button click event

它似乎不适用于click事件。 但MouseDown事件将为您提供一个具有按钮属性的MouseEventArgs。 那么也许您可以修改您的代码以使用MouseDown而不是点击?

 var btn = new Button(); btn.Text = "BUTTON"; btn.MouseDown += ((o, e) => { MessageBox.Show(e.Button.ToString()); }); 

只需尝试使用button1_MouseDown事件而不是buttons_Click Event.It将解决您的问题。

  private void button1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { //do something } if (e.Button == MouseButtons.Right) { //do something } } 

为左右按钮创建一个类boolean变量,默认为false。 当鼠标按下事件触发时,将变量设置为true并检查两者是否都为真。 当鼠标向上触发时将变量设置为false。

 public bool m_right = false; public bool m_left = false; private void MainForm_MouseDown(object sender, MouseEventArgs e) { m_objGraphics.Clear(SystemColors.Control); if (e.Button == MouseButtons.Left) m_left = true; if (e.Button == MouseButtons.Right) m_right = true; if (m_left == false || m_right == false) return; //do something here } private void MainForm_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) m_left = false; if (e.Button == MouseButtons.Right) m_right = false; } 

资源

以下代码对我很有用。 (请注意,我使用的EventArgs类型与问题中的函数类似)。

private void buttons_Click(object sender,EventArgs e){

 Type t = e.GetType(); if (t.Equals(typeof(MouseEventArgs))) { MouseEventArgs mouse = (MouseEventArgs)e; if (mouse.Button == MouseButtons.Left) { Console.WriteLine("****Left!!"); } if (mouse.Button == MouseButtons.Right) { Console.WriteLine("****Right!!"); } } 

}

目标:处理鼠标左键单击和鼠标右键单击。

问题:单击鼠标右键时,不会引发Click事件。 仅在按下鼠标左键然后释放时才会引发它。

解:

  1. 订阅Click和MouseRightButtonUp事件。

注意:当鼠标右键在按钮上释放时,会引发MouseRightButtonUp事件,而不管它是否在最初按下时是否在按钮上方。

注意B.这与这种情况无关,但你应该注意到Button对象的Click事件(按下并释放Mouse Left-button)同时处理MouseLeftButtonDown和MouseLeftButtonUp事件,因此这些事件都不是使用鼠标左键单击Button对象时处理。 如果您确实需要单独处理这些操作,则应订阅PreviewMouseLeftButtonDown和/或PreviewMouseLeftButtonUp。

  1. 然后你可以:

    一个。 有2个独立的事件处理程序,每个事件处理一个,或者

    湾 编写一个可以处理Click和MouseRightButtonUp事件的事件处理程序。 注意:Click事件需要一个处理程序,它接受RoutedEventArgs类型的参数,而MouseRightButtonUp事件需要一个MouseButtonEventArgs类型。 RoutedEventArgs和MouseButtonEventArgs都inheritance自EventArgs。 因此,让处理程序接受EventArgs。 然后,处理程序应确定是否已传递RoutedEventArgs(在这种情况下,它执行左键单击所需的操作),或者MouseButtonEventArgs(在这种情况下,它执行右键单击所需的操作)。

代码示例:

 myButton.MouseRightButtonUp += MenuItemButton_Click; myButton.Click += MenuItemButton_Click; internal void MenuItemButton_Click(object sender, EventArgs e) { var clickedButton = sender as Button; string EventArgsType = e.GetType().ToString(); //Remove leading qualification from Type ("System.Windows." or "System.Windows.Input.") EventArgsType = type.Substring(EventArgsType.LastIndexOf('.') + 1); switch (EventArgsType) { //Handle R-Click (RightMouseButtonUp) case "MouseButtonEventArgs": var MbeArgs = e as MouseButtonEventArgs; //Do stuff break; //Handle L-Click (Click) case "RoutedEventArgs": var ReArgs = e as RoutedEventArgs; //Do stuff break; default: break; } }