/// <summary> /// 卸载钩子 /// </summary> public void UnHook() { HookFunction.UnhookWindowsHookEx(hHook); }
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct)); bool flag = nCode < 0; int result; if (flag) { result = HookFunction.CallNextHookEx(hHook, nCode, wParam, lParam); } else { switch ((int)wParam) { case 513: { MouseButtons button = MouseButtons.Left; int clicks = 1; MouseDownEvent?.Invoke(this, new MouseEventArgs(button, clicks, point.X, point.Y, 0)); break; } case 514: { MouseButtons button = MouseButtons.Left; int clicks = 1; MouseUpEvent?.Invoke(this, new MouseEventArgs(button, clicks, point.X, point.Y, 0)); break; } case 516: { MouseButtons button = MouseButtons.Right; int clicks = 1; MouseDownEvent?.Invoke(this, new MouseEventArgs(button, clicks, point.X, point.Y, 0)); break; } case 517: { MouseButtons button = MouseButtons.Right; int clicks = 1; MouseUpEvent?.Invoke(this, new MouseEventArgs(button, clicks, point.X, point.Y, 0)); break; } case 519: { MouseButtons button = MouseButtons.Middle; int clicks = 1; MouseDownEvent?.Invoke(this, new MouseEventArgs(button, clicks, point.X, point.Y, 0)); break; } case 520: { MouseButtons button = MouseButtons.Middle; int clicks = 1; MouseUpEvent?.Invoke(this, new MouseEventArgs(button, clicks, point.X, point.Y, 0)); break; } } this.Point = new Point(mouseHookStruct.pt.x, mouseHookStruct.pt.y); result = HookFunction.CallNextHookEx(hHook, nCode, wParam, lParam); } return(result); }
/// <summary> /// 加载钩子 /// </summary> /// <returns></returns> public int SetHook() { hProc = new HookProc(MouseHookProc); hHook = HookFunction.SetWindowsHookEx(14, hProc, IntPtr.Zero, 0); return(hHook); }