C# winform 获取鼠标点击位置
程序员文章站
2022-03-30 16:16:16
说明:该篇随笔的代码内容并非出自本人,是在其他网站搜寻的,出处已经不记得了,本次随笔只为记录,目的帮助自己,帮助他人。 实现的原理也不做多的赘述,直接上代码。 第一个类是需要用到的Windows API public class Win32Api { [StructLayout(LayoutKind ......
说明:该篇随笔的代码内容并非出自本人,是在其他网站搜寻的,出处已经不记得了,本次随笔只为记录,目的帮助自己,帮助他人。
实现的原理也不做多的赘述,直接上代码。
第一个类是需要用到的windows api
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_lbuttondown = 0x201; private const int wm_rbuttondown = 0x204; 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; break; case wm_rbuttondown: button = mousebuttons.right; clickcount = 1; 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 getlocationform() { initializecomponent(); } mousehook mh; private void getlocationform_load(object sender, eventargs e) { mh = new mousehook(); mh.sethook(); mh.mousemoveevent += mh_mousemoveevent; mh.mouseclickevent += mh_mouseclickevent; } private void mh_mouseclickevent(object sender, mouseeventargs e) { if (e.button == mousebuttons.right) { //messagebox.show(e.x + "-" + e.y); textbox1.text = e.x.tostring(); textbox2.text = e.y.tostring(); } } 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(); }
上一篇: 赵光义为什么射杀了花蕊夫人?
下一篇: C#获取文件夹下所有的文件名称