public void UnHook() { Win32Api.UnhookWindowsHookEx(hHook); }
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct)); if (nCode < 0) { return(Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam)); } else { if (MouseClickEvent != null) { MouseButtons button = MouseButtons.None; int clickCount = 0; switch ((Int32)wParam) { case WM_LBUTTONDOWN: button = MouseButtons.Left; wasDown = true; clickCount = 1; if (MouseDownEvent != null) { MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } break; case WM_RBUTTONDOWN: button = MouseButtons.Right; wasDown = true; clickCount = 1; if (MouseDownEvent != null) { MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } break; case WM_MBUTTONDOWN: button = MouseButtons.Middle; wasDown = true; clickCount = 1; if (MouseDownEvent != null) { MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } break; case WM_LBUTTONUP: button = MouseButtons.Left; clickCount = 1; if (wasDown) { wasDown = false; MouseClickEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } if (MouseDownEvent != null) { MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } break; case WM_RBUTTONUP: button = MouseButtons.Right; clickCount = 1; if (wasDown) { wasDown = false; MouseClickEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } if (MouseDownEvent != null) { MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } break; case WM_MBUTTONUP: button = MouseButtons.Middle; clickCount = 1; if (wasDown) { wasDown = false; MouseClickEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } if (MouseDownEvent != null) { MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0)); } break; } } this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y); return(Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam)); } }
public int SetHook() { hProc = new Win32Api.HookProc(MouseHookProc); hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); return(hHook); }