c#鼠标钩子全解
发布时间:2025-12-09 22:31:20
浏览次数:6
using System;using System.Runtime.InteropServices;//调用操作系统动态链接库using System.Diagnostics;using System.Windows.Forms;namespace util{public class 鼠标钩子类{//使用方法//private 鼠标钩子类 hook = new 鼠标钩子类();//hook.InstallHook(this.拦截函数);//pubic void 拦截函数(鼠标钩子类.鼠标信息结构体 hookstruct,out bool handle){}//if(hook !=null) hook.UninstallHook();public const int WH_MOUSE_LL = 14;//全局钩子鼠标为14public const int WM_MOUSEMOVE = 0x200; //移动鼠标时发生,同WM_MOUSEFIRSTpublic const int WM_LBUTTONDOWN = 0x201; //按下鼠标左键public const int WM_LBUTTONUP = 0x202; //释放鼠标左键public const int WM_LBUTTONDBLCLK = 0x203;//双击鼠标左键public const int WM_RBUTTONDOWN = 0x204;//按下鼠标右键public const int WM_RBUTTONUP = 0x205;//释放鼠标右键public const int WM_RBUTTONDBLCLK = 0x206; //双击鼠标右键public const int WM_MBUTTONDOWN = 0x207; //按下鼠标中键public const int WM_MBUTTONUP = 0x208;//释放鼠标中键public const int WM_MBUTTONDBLCLK = 0x209;//双击鼠标中键public delegate int HookProc(int nCode, int wParam, IntPtr lParam);public delegate void 调用端函数(鼠标信息结构体 param, out bool handle);private static int 是否以安装 = 0;private HookProc 执行函数引用;[StructLayout(LayoutKind.Sequential)]public class POINT{public int x;public int y;}[StructLayout(LayoutKind.Sequential)]public class 鼠标信息结构体{public POINT pt;public int hwnd;public int wHitTestCode;public int dwExtraInfo;}//设置钩子 //idHook为13代表键盘钩子为14代表鼠标钩子,lpfn为函数指针,指向需要执行的函数,hInstance为指向进程块的指针,threadId默认为0就可以了[DllImport("user32.dll")]private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);//取消钩子 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]private static extern bool UnhookWindowsHookEx(int idHook);//调用下一个钩子 [DllImport("user32.dll")]private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);//获取当前线程ID(获取进程块)[DllImport("kernel32.dll")]private static extern int GetCurrentThreadId();//Gets the main module for the associated process.[DllImport("kernel32.dll")]private static extern IntPtr GetModuleHandle(string name);private IntPtr 进程块 = IntPtr.Zero;//构造器public 鼠标钩子类() { }//外部调用的鼠标处理事件private static 调用端函数 调用函数引用 = null;public void InstallHook(调用端函数 clientMethod){调用函数引用 = clientMethod;// 安装鼠标钩子 if (是否以安装 == 0){执行函数引用 = new HookProc(执行函数);进程块 = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);是否以安装 = SetWindowsHookEx(WH_MOUSE_LL,执行函数引用,进程块,0);//如果设置钩子失败. if (是否以安装 == 0) UninstallHook();}}public void UninstallHook(){if (是否以安装 != 0){bool ret = UnhookWindowsHookEx(是否以安装);if (ret) 是否以安装 = 0;}}//钩子事件内部调用,调用_clientMethod方法转发到客户端应用。private static int 执行函数(int nCode, int wParam, IntPtr lParam){if (nCode >= 0 && wParam == (int)WM_LBUTTONUP){鼠标信息结构体 MyMouseHookStruct = (鼠标信息结构体)Marshal.PtrToStructure(lParam, typeof(鼠标信息结构体));if (是否以安装 != 0){bool ret = UnhookWindowsHookEx(是否以安装);if (ret) 是否以安装 = 0;}if (调用函数引用 != null){bool handle = false;//调用客户提供的事件处理程序。调用函数引用(MyMouseHookStruct, out handle);if (handle) return 1; //1:表示拦截键盘,return 退出}}return CallNextHookEx(是否以安装, nCode, wParam, lParam); //拦截下一个鼠标事件}}}