public class MouseHookEventArgs : EventArgs
public bool Handle { get; set; }
public MouseHookEventArgs(MouseMessages mouseMessage)
MouseMessage = mouseMessage;
public MouseMessages MouseMessage { get; }
public enum MouseMessages
public static class MouseHook
private delegate IntPtr MouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static MouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private const int WH_MOUSE_LL = 14;
private enum MouseMessages
[StructLayout(LayoutKind.Sequential)]
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
private IntPtr dwExtraInfo;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
MouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
/// <param name="moduleName"></param>
public static void Start(string moduleName)
Debug.WriteLine($"模块 {moduleName} 开启全局鼠标钩子");
_hookID = SetHook(_proc);
public static void Stop()
UnhookWindowsHookEx(_hookID);
private static IntPtr SetHook(MouseProc proc)
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
return SetWindowsHookEx(WH_MOUSE_LL, proc,
Kernel32.GetModuleHandle(curModule.ModuleName), 0);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
MouseHookEventArgs mouseHookEventArgs = null;
mouseHookEventArgs=(new MouseHookEventArgs(MouseHookEventArgs.MouseMessages.MouseMove));
mouseHookEventArgs=(new MouseHookEventArgs(MouseHookEventArgs.MouseMessages.MouseDown));
mouseHookEventArgs=(new MouseHookEventArgs(MouseHookEventArgs.MouseMessages.MouseUp));
//MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
if (mouseHookEventArgs != null)
OnMouseEvent(mouseHookEventArgs);
if (mouseHookEventArgs.Handle)
return CallNextHookEx(_hookID, nCode, wParam, lParam);
public static event EventHandler<MouseHookEventArgs> MouseEvent;
private static void OnMouseEvent(MouseHookEventArgs e)
MouseEvent?.Invoke(null, e);