protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam) { if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null)) { MouseLlHookStruct mouseHookStruct = (MouseLlHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLlHookStruct)); MouseButtons button = GetButton(wParam); MouseEventType eventType = GetEventType(wParam); MouseEventArgs e = new MouseEventArgs(button, (eventType == MouseEventType.DoubleClick ? 2 : 1), mouseHookStruct.pt.x, mouseHookStruct.pt.y, (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0)); // Prevent multiple Right Click events (this probably happens for popup menus) if (button == MouseButtons.Right && mouseHookStruct.flags != 0) { eventType = MouseEventType.None; } switch (eventType) { case MouseEventType.MouseDown: if (MouseDown != null) { MouseDown(this, e); } break; case MouseEventType.MouseUp: if (Click != null) { Click(this, new EventArgs()); } if (MouseUp != null) { MouseUp(this, e); } break; case MouseEventType.DoubleClick: if (DoubleClick != null) { DoubleClick(this, new EventArgs()); } break; case MouseEventType.MouseWheel: if (MouseWheel != null) { MouseWheel(this, e); } break; case MouseEventType.MouseMove: if (MouseMove != null) { MouseMove(this, e); } break; } } //return -1; //屏蔽 return(CallNextHookEx(HandleToHook, nCode, wParam, lParam)); }
private static int MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { MouseLlHookStruct mouseHookStruct = (MouseLlHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLlHookStruct)); MouseButtons button = MouseButtons.None; var mouseDelta = 0; var clickCount = 0; var mouseDown = false; var mouseUp = false; switch (wParam) { case WM_LBUTTONDOWN: mouseDown = true; button = MouseButtons.Left; clickCount = 1; break; //case WM_LBUTTONUP: // mouseUp = true; // button = MouseButtons.Left; // clickCount = 1; // break; //case WM_LBUTTONDBLCLK: // button = MouseButtons.XButton1; // clickCount = 2; // break; //case WM_RBUTTONDOWN: // mouseDown = true; // button = MouseButtons.Right; // clickCount = 1; // break; //case WM_RBUTTONUP: // mouseUp = true; // button = MouseButtons.Right; // clickCount = 1; // break; //case WM_RBUTTONDBLCLK: // button = MouseButtons.Right; // clickCount = 2; // break; //case WM_MOUSEWHEEL: // mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); // break; } var e = new MouseEventExtArgs(button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta); if (e.Button == MouseButtons.Left) { var point = new System.Drawing.Point(); point.X = mouseHookStruct.Point.X; point.Y = mouseHookStruct.Point.Y; PositionClick.Add(point); } if (SMouseUp != null && mouseUp) { SMouseUp.Invoke(null, e); } if (SMouseDown != null && mouseDown) { SMouseDown.Invoke(null, e); } if (SMouseClick != null && clickCount > 0) { SMouseClick.Invoke(null, e); } if (SMouseClickExt != null && clickCount > 0) { SMouseClickExt.Invoke(null, e); } if (SMouseDoubleClick != null && clickCount == 2) { SMouseDoubleClick.Invoke(null, e); } if (SMouseWheel != null && mouseDelta != 0) { SMouseWheel.Invoke(null, e); } if ((SMouseMove != null || SMouseMoveExt != null) && (_mOldX != mouseHookStruct.Point.X || _mOldY != mouseHookStruct.Point.Y)) { _mOldX = mouseHookStruct.Point.X; _mOldY = mouseHookStruct.Point.Y; SMouseMove?.Invoke(null, e); SMouseMoveExt?.Invoke(null, e); } if (e.Handled) { return(-1); } } return(CallNextHookEx(_sMouseHookHandle, nCode, wParam, lParam)); }