C# 如何获得右键单击鼠标事件?将 EventArgs 更改为 MouseEventArgs 会导致 Form1Designer 中出现错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19448346/
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 to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?
提问by Danny
I have a method to detect the left click event that visual studio made by double clicking on the form.
我有一种方法来检测 Visual Studio 通过双击表单制作的左键单击事件。
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Left click");
}
I want to have a right-click event by right-clicking on the same object.
我想通过右键单击同一对象来进行右键单击事件。
I read online that you can use this switch:
我在网上看到你可以使用这个开关:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right){MessageBox.Show("Right click");}
if (e.Button == System.Windows.Forms.MouseButtons.Left){MessageBox.Show("Left click");}
}
The trouble is that when I do e.Button
it has has yields an error error:
麻烦的是,当我这样做时,e.Button
它产生了一个错误错误:
System.EventArgs
does not contain a definition forButton
...
System.EventArgs
不包含Button
...的定义
So I fix this by changing the EventArgs.e
to MouseEventArgs.e
所以我通过改变EventArgs.e
来解决这个问题MouseEventArgs.e
But then there is a new error in Form1Designer where the event line is:
但是随后在 Form1Designer 中出现了一个新错误,其中事件行是:
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
The error says:
错误说:
No overload for
pictureBox1_Click
matches delegateSystem.EventHandler
pictureBox1_Click
匹配委托没有过载System.EventHandler
How do I fix this? Thanks for reading
我该如何解决?谢谢阅读
采纳答案by Sriram Sakthivel
You should introduce a cast inside the click
event handler
您应该在click
事件处理程序中引入强制转换
MouseEventArgs me = (MouseEventArgs) e;
回答by Coder
回答by progpow
Use MouseClick event instead of Click
使用 MouseClick 事件代替 Click
回答by ThunderGr
For me neither the MouseClick or Click event worked, because the events, simply, are not called when you right click. The quick way to do it is:
对我来说,MouseClick 或 Click 事件都不起作用,因为当您单击鼠标右键时,不会调用这些事件。快速的方法是:
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//do something here
}
else//left or middle click
{
//do something here
}
}
You can modify that to do exactly what you want depended on the arguments' values.
您可以根据参数的值修改它以完全执行您想要的操作。
WARNING:There is one catch with only using the mouse up event. if you mousedown on the control and then you move the cursor out of the control to release it, you still get the event fired. In order to avoid that, you should also make sure that the mouse up occurs within the control in the event handler. Checking whether the mouse cursor coordinates are within the control's rectangle before you check the buttons will do it properly.
警告:仅使用鼠标向上事件有一个问题。如果您在控件上按下鼠标,然后将光标移出控件以释放它,您仍然会触发该事件。为了避免这种情况,您还应该确保鼠标向上发生在事件处理程序的控件内。在检查按钮之前检查鼠标光标坐标是否在控件的矩形内将正确执行此操作。
回答by haoming weng
See the code below, this is the complete code about getting a mouse event(rightclick, leftclick) And you can DIY this code and make it on your own.
看下面的代码,这是获取鼠标事件(右键单击,左键单击)的完整代码,你可以DIY这段代码,自己制作。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Demo_mousehook_csdn
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MouseHook mh;
private void Form1_Load(object sender, EventArgs e)
{
mh = new MouseHook();
mh.SetHook();
mh.MouseMoveEvent += mh_MouseMoveEvent;
mh.MouseClickEvent += mh_MouseClickEvent;
mh.MouseDownEvent += mh_MouseDownEvent;
mh.MouseUpEvent += mh_MouseUpEvent;
}
private void mh_MouseDownEvent(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
richTextBox1.AppendText("Left Button Press\n");
}
if (e.Button == MouseButtons.Right)
{
richTextBox1.AppendText("Right Button Press\n");
}
}
private void mh_MouseUpEvent(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
richTextBox1.AppendText("Left Button Release\n");
}
if (e.Button == MouseButtons.Right)
{
richTextBox1.AppendText("Right Button Release\n");
}
}
private void mh_MouseClickEvent(object sender, MouseEventArgs e)
{
//MessageBox.Show(e.X + "-" + e.Y);
if (e.Button == MouseButtons.Left)
{
string sText = "(" + e.X.ToString() + "," + e.Y.ToString() + ")";
label1.Text = sText;
}
}
private void mh_MouseMoveEvent(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
textBox1.Text = x + "";
textBox2.Text = y + "";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
mh.UnHook();
}
private void Form1_FormClosed_1(object sender, FormClosedEventArgs e)
{
mh.UnHook();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
public class Win32Api
{
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
}
public class MouseHook
{
private Point point;
private Point Point
{
get { return point; }
set
{
if (point != value)
{
point = value;
if (MouseMoveEvent != null)
{
var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
MouseMoveEvent(this, e);
}
}
}
}
private int hHook;
private const int WM_MOUSEMOVE = 0x200;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_RBUTTONDOWN = 0x204;
private const int WM_MBUTTONDOWN = 0x207;
private const int WM_LBUTTONUP = 0x202;
private const int WM_RBUTTONUP = 0x205;
private const int WM_MBUTTONUP = 0x208;
private const int WM_LBUTTONDBLCLK = 0x203;
private const int WM_RBUTTONDBLCLK = 0x206;
private const int WM_MBUTTONDBLCLK = 0x209;
public const int WH_MOUSE_LL = 14;
public Win32Api.HookProc hProc;
public MouseHook()
{
this.Point = new Point();
}
public int SetHook()
{
hProc = new Win32Api.HookProc(MouseHookProc);
hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
return hHook;
}
public void UnHook()
{
Win32Api.UnhookWindowsHookEx(hHook);
}
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));
if (nCode < 0)
{
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
if (MouseClickEvent != null)
{
MouseButtons button = MouseButtons.None;
int clickCount = 0;
switch ((Int32)wParam)
{
case WM_LBUTTONDOWN:
button = MouseButtons.Left;
clickCount = 1;
MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_RBUTTONDOWN:
button = MouseButtons.Right;
clickCount = 1;
MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_MBUTTONDOWN:
button = MouseButtons.Middle;
clickCount = 1;
MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_LBUTTONUP:
button = MouseButtons.Left;
clickCount = 1;
MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_RBUTTONUP:
button = MouseButtons.Right;
clickCount = 1;
MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_MBUTTONUP:
button = MouseButtons.Middle;
clickCount = 1;
MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
}
var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
MouseClickEvent(this, e);
}
this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
public event MouseMoveHandler MouseMoveEvent;
public delegate void MouseClickHandler(object sender, MouseEventArgs e);
public event MouseClickHandler MouseClickEvent;
public delegate void MouseDownHandler(object sender, MouseEventArgs e);
public event MouseDownHandler MouseDownEvent;
public delegate void MouseUpHandler(object sender, MouseEventArgs e);
public event MouseUpHandler MouseUpEvent;
}
}
You can download the demo And the tutorial here : C# Mouse Hook Demo
你可以在这里下载演示和教程:C# Mouse Hook Demo
回答by Hassaan Raza
This would definitely help Many!
这肯定会帮助很多人!
private void axWindowsMediaPlayer1_ClickEvent(object sender, AxWMPLib._WMPOCXEvents_ClickEvent e)
{
if(e.nButton==2)
{
contextMenuStrip1.Show(MousePosition);
}
}
[e.nbutton==2 ]is like[e.button==MouseButtons.Right ]
[e.nbutton==2 ]就像[e.button==MouseButtons.Right ]
回答by Ralph Oliveros
Use MouseDown event
使用鼠标按下事件
if(e.Button == MouseButton.Right)