windows操作系统 C#编写鼠标钩子

要做一个东西,由于捕获不到相应的事件,而这个东西又要用鼠标操作,所以找了下质料可以用鼠标钩子实现。自己也没用过鼠标钩子,看了别人的例子,实践到项目中就解决问题了。把代码记录下来以备以后用和更深入的学习。
    class MouseHook{//枚举定义public enum WH_Codes : int{/// /// 底层鼠标钩子/// WH_MOUSE_LL = 14}public enum WM_MOUSE : int{/// /// 鼠标开始/// WM_MOUSEFIRST = 0x200,/// /// 鼠标移动/// WM_MOUSEMOVE = 0x200,/// /// 左键按下/// WM_LBUTTONDOWN = 0x201,/// /// 左键释放/// WM_LBUTTONUP = 0x202,/// /// 左键双击/// WM_LBUTTONDBLCLK = 0x203,/// /// 右键按下/// WM_RBUTTONDOWN = 0x204,/// /// 右键释放/// WM_RBUTTONUP = 0x205,/// /// 右键双击/// WM_RBUTTONDBLCLK = 0x206,/// /// 中键按下/// WM_MBUTTONDOWN = 0x207,/// /// 中键释放/// WM_MBUTTONUP = 0x208,/// /// 中键双击/// WM_MBUTTONDBLCLK = 0x209,/// /// 滚轮滚动/// /// WINNT4.0以上才支持此消息WM_MOUSEWHEEL = 0x020A}[StructLayout(LayoutKind.Sequential)]public struct POINT{public int X;public int Y;}/// /// 鼠标钩子事件结构定义/// /// 详细说明请参考MSDN中关于 MSLLHOOKSTRUCT 的说明[StructLayout(LayoutKind.Sequential)]public struct MouseHookStruct{public POINT Point;public UInt32 MouseData;public UInt32 Flags;public UInt32 Time;public UInt32 ExtraInfo;}#region DLL导入/// /// 安装钩子/// /// /// /// /// /// [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]public static extern IntPtr SetWindowsHookEx(WH_Codes idHook, HookProc lpfn,IntPtr pInstance, int threadId);/// /// 卸载钩子/// /// /// [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);/// /// 传递钩子/// /// /// /// /// /// [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode,Int32 wParam, IntPtr lParam);#endregion DLL导入//鼠标钩子句柄private IntPtr m_pMouseHook = IntPtr.Zero;/// /// 钩子委托声明/// /// /// /// /// public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);/// /// 鼠标钩子委托实例/// /// /// 不要试图省略此变量,否则将会导致/// 激活 CallbackOnCollectedDelegate 托管调试助手 (MDA)。 /// 详细请参见MSDN中关于 CallbackOnCollectedDelegate 的描述/// private HookProc m_MouseHookProcedure;#region 事件定义/// /// 鼠标移动事件/// /// 当鼠标移动或者滚轮滚动时触发public event MouseEventHandler OnMouseMove;/// /// 鼠标按下事件/// public event MouseEventHandler OnMouseDown;/// /// 按键释放事件/// public event MouseEventHandler OnMouseUp;#endregion#region 私有方法/// /// 鼠标钩子处理函数/// /// /// /// /// private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam){if ((nCode >= 0) && (this.OnMouseMove != null)&& (wParam == (int)WM_MOUSE.WM_MOUSEMOVE ||wParam == (int)WM_MOUSE.WM_MOUSEWHEEL)){//MouseHookStruct MouseInfo = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));this.OnMouseMove(null,null);}if ((nCode >= 0) && (this.OnMouseDown != null)&&(wParam==(int)WM_MOUSE.WM_LBUTTONDOWN||wParam==(int)WM_MOUSE.WM_RBUTTONDOWN)) {MouseButtons button = MouseButtons.None;switch (wParam){case (int)WM_MOUSE.WM_LBUTTONDOWN://case WM_LBUTTONUP: //case WM_LBUTTONDBLCLK: button = MouseButtons.Left;break;case (int)WM_MOUSE.WM_RBUTTONDOWN://case WM_RBUTTONUP: //case WM_RBUTTONDBLCLK: button = MouseButtons.Right;break;}MouseEventArgs e = new MouseEventArgs(button,0, 0,0,0);this.OnMouseDown(null, e);}if ((nCode >= 0) && (this.OnMouseDown != null)&& (wParam == (int)WM_MOUSE.WM_LBUTTONUP || wParam == (int)WM_MOUSE.WM_RBUTTONUP)){MouseButtons button = MouseButtons.None;switch (wParam){case (int)WM_MOUSE.WM_LBUTTONUP://case WM_LBUTTONUP: //case WM_LBUTTONDBLCLK: button = MouseButtons.Left;break;case (int)WM_MOUSE.WM_RBUTTONUP://case WM_RBUTTONUP: //case WM_RBUTTONDBLCLK: button = MouseButtons.Right;break;}MouseEventArgs e = new MouseEventArgs(button,0,0,0,0);this.OnMouseUp(null, e);}return CallNextHookEx(this.m_pMouseHook, nCode, wParam, lParam);}#endregion#region 公共方法/// /// 安装钩子/// /// public bool InstallHook(){IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);//pInstance = (IntPtr)4194304;// IntPtr pInstanc2 = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly());// Assembly.GetExecutingAssembly().GetModules()[0]// 假如没有安装鼠标钩子if (this.m_pMouseHook == IntPtr.Zero){this.m_MouseHookProcedure = new HookProc(this.MouseHookProc);this.m_pMouseHook = SetWindowsHookEx(WH_Codes.WH_MOUSE_LL, this.m_MouseHookProcedure, pInstance, 0);if (this.m_pMouseHook == IntPtr.Zero){this.UnInstallHook();return false;}}return true;}/// /// 卸载钩子/// /// public bool UnInstallHook(){bool result = true;if (this.m_pMouseHook != IntPtr.Zero){result = (UnhookWindowsHookEx(this.m_pMouseHook) && result);this.m_pMouseHook = IntPtr.Zero;}return result;}#endregion}

用该封装的类,首先注册钩子,然后添加相应的鼠标事件。其中鼠标事件什么时候调用和传递的参数都是在鼠标处理函数 MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)里面完成,本例中由于鼠标处理不需要参数,仅需要获取按下的是左键还是又见,所以原本定义的MouseEventArg参数类其他属性都赋值为0。而鼠标处理函数MouseHookProc的调用则是在鼠标钩子注册方法里面调用的。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部