Microsoft Visual Studio 2010做的C#简单的鼠标钩子应用实例实例,实现实时监控鼠标位置(需要编译后,在Release文件夹那里运行)主要代码:
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//定义钩子句柄
public static int m_iHock = 0;
//定义钩子类型
public const int WH_MOUSE_LL = 14;
public HookProc HookProcVar;
//安装钩子
[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);
[StructLayout(LayoutKind.Sequential)]
1