protected int MouseHook(int ncode, IntPtr wParam, IntPtr lParam) { HookInfo hInfo = GetInfoByThread(); int res; bool allowFutureProcess = true; if (ncode == 0) { MOUSEHOOKSTRUCT hookStr = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT)); if (!hInfo.inMouseHook && lParam != IntPtr.Zero) { try { Control ctrl = Control.FromHandle(hookStr.hwnd); hInfo.inMouseHook = true; allowFutureProcess = !InternalPreFilterMessage(hInfo, wParam.ToInt32(), ctrl, hookStr.hwnd, IntPtr.Zero, new IntPtr((hookStr.Pt.X << 16) | hookStr.Pt.Y)); } finally { hInfo.inMouseHook = false; } } else { return(CallNextHookEx(hInfo.mouseHookHandle, ncode, wParam, lParam)); } } res = CallNextHookEx(hInfo.mouseHookHandle, ncode, wParam, lParam); if (!allowFutureProcess) { res = -1; } return(res); }
private int MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //把参数lParam在内存中指向的数据转换为MOUSEHOOKSTRUCT结构 MOUSEHOOKSTRUCT mouse = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));//鼠标 mea = new MouseEventArgs(MouseButtons.Left, 1, mouse.pt.X, mouse.pt.Y, 0); //这句为了看鼠标的位置 this.Text = "MousePosition:" + mouse.pt.ToString(); mousdraw_MouseMove(mea); if (wParam == WM_LEFT_RBUTTONDOWN || wParam == WM_LEFT_RBUTTONUP || wParam == WM_RIGHT_RBUTTONDOWN || wParam == WM_RIGHT_RBUTTONUP) { //鼠标按下或者释放时候截获 if (newTaskBarRect.Contains(mouse.pt)) { //当鼠标在任务栏的范围内 //如果返回1,则结束消息,这个消息到此为止,不再传递。 //如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者 return(0); } else if (wParam == WM_LEFT_RBUTTONDOWN) { return(mousdraw_MouseDown(mea)); } else if (wParam == WM_LEFT_RBUTTONUP) { return(mousdraw_MouseUp(mea)); } else { return(1); } } } return(CallNextHookEx(hMouseHook, nCode, wParam, lParam)); }
void MouseAction(MouseEvents @event, MOUSEHOOKSTRUCT data, int delta) { switch (@event) { case MouseEvents.LBUTTONDOWN: mouseKeyStates[0].state = mouseKeyStates[0].state == ManagerKeyState.Down ? ManagerKeyState.Hold : ManagerKeyState.Down; break; case MouseEvents.LBUTTONUP: mouseKeyStates[0].state = ManagerKeyState.Up; break; case MouseEvents.MOUSEMOVE: break; case MouseEvents.MOUSEWHEEL: //MessageBox.Show("Test :" + delta); break; case MouseEvents.MOUSEHWHEEL: break; case MouseEvents.RBUTTONDOWN: mouseKeyStates[1].state = mouseKeyStates[1].state == ManagerKeyState.Down ? ManagerKeyState.Hold : ManagerKeyState.Down; break; case MouseEvents.RBUTTONUP: mouseKeyStates[1].state = ManagerKeyState.Up; break; default: break; } }
/// <summary> /// 마우스 프로시저 처리하기 /// </summary> /// <param name="code">코드</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> private static IntPtr MouseProc(int code, IntPtr wordParameter, IntPtr longParameter) { if (code >= 0) { _mouseHookStructure = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure ( longParameter, typeof(MOUSEHOOKSTRUCT) ); MouseMessage mouseMessage = (MouseMessage)wordParameter; if (UseGlobal) { if (mouseMessage == MouseMessage.WM_LBUTTONDOWN || mouseMessage == MouseMessage.WM_LBUTTONUP) { MouseClick?.Invoke(_mouseHookStructure.Location, mouseMessage); if (mouseMessage == MouseMessage.WM_LBUTTONDOWN && IsHookingArea()) { return((IntPtr)1); } } } } return(WIN32Helper.CallNextHookEx(_mouseHookHandle, code, wordParameter, longParameter)); }
public unsafe void init_MOUSHOOKSTRU() { void * tagMOUSE = this.lParam.ToPointer(); MOUSEHOOKSTRUCT *p = (MOUSEHOOKSTRUCT *)tagMOUSE; MOUSEHOOKSTRUCT = *p; init = true; }
protected IntPtr HookProcHandler(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { MessageType flag = (MessageType)wParam; MOUSEHOOKSTRUCT mouseData = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT)); Console.WriteLine("Flag:{0} mouseData:{1}", flag, mouseData); } return(User32.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)); }
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { MOUSEHOOKSTRUCT pMouseStruct = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT)); CallMouseEvent((MouseEvents)wParam, pMouseStruct, NativeMethods.GET_WHEEL_DELTA_WPARAM(wParam)); } return(WinApiFunctions.CallNextHookEx(_hookID, nCode, wParam, lParam)); }
private IntPtr MouseHook(int code, IntPtr wparam, IntPtr lparam) { MOUSEHOOKSTRUCT mh = (MOUSEHOOKSTRUCT )Marshal.PtrToStructure(lparam, typeof(MOUSEHOOKSTRUCT)); // if user set focus on edit control embedded by us then do not open DropDown box if (mh.hwnd == Handle && wparam == (IntPtr)Msg.WM_LBUTTONDOWN && m_bDropDown == false && ReadOnly == false) { m_bSkipClick = true; } return(WindowsAPI.CallNextHookEx(m_mouseHookHandle, code, wparam, lparam)); }
private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam) { //如果正常运行并且用户要监听鼠标的消息 if ((nCode >= 0) && (OnMouseActivity != null)) { MouseButtons button = MouseButtons.None; int clickCount = 0; switch (wParam) { case Win32.Const.WM.WM_LBUTTONDOWN: button = MouseButtons.Left; clickCount = 1; break; case Win32.Const.WM.WM_LBUTTONUP: button = MouseButtons.Left; clickCount = 1; break; case Win32.Const.WM.WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case Win32.Const.WM.WM_RBUTTONDOWN: button = MouseButtons.Right; clickCount = 1; break; case Win32.Const.WM.WM_RBUTTONUP: button = MouseButtons.Right; clickCount = 1; break; case Win32.Const.WM.WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; } //从回调函数中得到鼠标的信息 MOUSEHOOKSTRUCT MyMOUSEHOOKSTRUCT = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT)); MouseEventArgs e = new MouseEventArgs(button, clickCount, MyMOUSEHOOKSTRUCT.Pt.X, MyMOUSEHOOKSTRUCT.Pt.Y, 0); OnMouseActivity(this, e); } return(Win32.NativeMethods.CallNextHookEx((int)hMouseHook, nCode, wParam, lParam)); }
internal MouseHookEventArgs(IntPtr wparam, IntPtr lparam) { if (!Enum.IsDefined(typeof(MouseMessages), wparam.ToInt32())) { message = MouseMessages.Unknown; } else { message = (MouseMessages)wparam.ToInt32(); } MOUSEHOOKSTRUCT hs = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lparam, typeof(MOUSEHOOKSTRUCT)); point = new Point(hs.pt.x, hs.pt.y); hwnd = hs.hwnd; hitTestCode = (HitTestCodes)hs.hitTestCode; extraInfo = hs.extraInfo; }
IntPtr MouseHook(int code, IntPtr wparam, IntPtr lparam) { MOUSEHOOKSTRUCT mh = (MOUSEHOOKSTRUCT )Marshal.PtrToStructure(lparam, typeof(MOUSEHOOKSTRUCT)); if (mh.hwnd != Handle && !DroppedDown && (wparam == (IntPtr)Msg.WM_LBUTTONDOWN || wparam == (IntPtr)Msg.WM_RBUTTONDOWN) || wparam == (IntPtr)Msg.WM_NCLBUTTONDOWN) { // Loose focus WindowsAPI.SetFocus(IntPtr.Zero); } else if (mh.hwnd != Handle && !DroppedDown && (wparam == (IntPtr)Msg.WM_LBUTTONUP || wparam == (IntPtr)Msg.WM_RBUTTONUP) || wparam == (IntPtr)Msg.WM_NCLBUTTONUP) { WindowsAPI.SetFocus(IntPtr.Zero); } return(WindowsAPI.CallNextHookEx(mouseHookHandle, code, wparam, lparam)); }
private int MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //把参数lParam在内存中指向的数据转换为MOUSEHOOKSTRUCT结构 MOUSEHOOKSTRUCT mouse = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));//鼠标 //这句为了看鼠标的位置 this.Text = "MousePosition:" + mouse.pt.ToString(); if (wParam == Hooker.WM_RBUTTONDOWN || wParam == Hooker.WM_RBUTTONUP) { //鼠标按下或者释放时候截获 if (newTaskBarRect.Contains(mouse.pt)) { //当鼠标在任务栏的范围内 //如果返回1,则结束消息,这个消息到此为止,不再传递。 //如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者 return(1); } } } return(Hooker.CallNextHookEx(Hooker.hMouseHook, nCode, wParam, lParam)); }
/// <summary> /// Mouse hook procedure /// The system calls this function whenever an application calls the /// GetMessage or PeekMessage function and there is a mouse message to be /// processed. /// </summary> /// <param name="nCode"> /// The hook code passed to the current hook procedure. /// When nCode equals HC_ACTION, the wParam and lParam parameters contain /// information about a mouse message. /// When nCode equals HC_NOREMOVE, the wParam and lParam parameters /// contain information about a mouse message, and the mouse message has /// not been removed from the message queue. (An application called the /// PeekMessage function, specifying the PM_NOREMOVE flag.) /// </param> /// <param name="wParam"> /// Specifies the identifier of the mouse message. /// </param> /// <param name="lParam">Pointer to a MOUSEHOOKSTRUCT structure.</param> /// <returns></returns> /// <see cref="http://msdn.microsoft.com/en-us/library/ms644988.aspx"/> private int MouseProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode == HookCodes.HC_ACTION) { // Marshal the MOUSEHOOKSTRUCT data from the callback lParam MOUSEHOOKSTRUCT mouseHookStruct = (MOUSEHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT)); // Get the mouse WM from the wParam parameter MouseMessage wmMouse = (MouseMessage)wParam; // Display the current mouse coordinates and the message String log = String.Format("X = {0} Y = {1} ({2})\r\n", mouseHookStruct.pt.x, mouseHookStruct.pt.y, wmMouse); this.tbLog.AppendText(log); } // Pass the hook information to the next hook procedure in chain return(NativeMethods.CallNextHookEx(hLocalMouseHook, nCode, wParam, lParam)); }
private int HookCallback(int code, IntPtr wParam, ref MOUSEHOOKSTRUCT lParam) { switch ((MouseMessage)wParam) { case MouseMessage.WM_LBUTTONDOWN: case MouseMessage.WM_RBUTTONDOWN: case MouseMessage.WM_MBUTTONDOWN: KeyDown?.Invoke(this, new MouseHookEventArgs(MouseButtonsConverter.GetButtonName((MouseMessage)wParam))); break; case MouseMessage.WM_LBUTTONUP: case MouseMessage.WM_RBUTTONUP: case MouseMessage.WM_MBUTTONUP: KeyUp?.Invoke(this, new MouseHookEventArgs(MouseButtonsConverter.GetButtonName((MouseMessage)wParam))); break; case MouseMessage.WM_XBUTTONDOWN when lParam.hwnd.Equals((IntPtr)0x00010000): KeyDown?.Invoke(this, new MouseHookEventArgs($"{MouseButtonsConverter.GetButtonName((MouseMessage)wParam)} 1")); break; case MouseMessage.WM_XBUTTONUP when lParam.hwnd.Equals((IntPtr)0x00010000): KeyUp?.Invoke(this, new MouseHookEventArgs($"{MouseButtonsConverter.GetButtonName((MouseMessage)wParam)} 1")); break; case MouseMessage.WM_XBUTTONDOWN when lParam.hwnd.Equals((IntPtr)0x00020000): KeyDown?.Invoke(this, new MouseHookEventArgs($"{MouseButtonsConverter.GetButtonName((MouseMessage)wParam)} 2")); break; case MouseMessage.WM_XBUTTONUP when lParam.hwnd.Equals((IntPtr)0x00020000): KeyUp?.Invoke(this, new MouseHookEventArgs($"{MouseButtonsConverter.GetButtonName((MouseMessage)wParam)} 2")); break; } return(CallNextHookEx(_hookHandle, code, wParam, ref lParam)); }
private static extern int CallNextHookEx(IntPtr hook, int code, IntPtr wParam, ref MOUSEHOOKSTRUCT lParam);
private int HookCallback(int code, IntPtr wParam, ref MOUSEHOOKSTRUCT lParam) { switch ((MouseMessages)wParam) { case MouseMessages.WM_LBUTTONDOWN: KeyDown?.Invoke(this, new HookEventArgs("Mouse LB")); break; case MouseMessages.WM_LBUTTONUP: KeyUp?.Invoke(this, new HookEventArgs("Mouse LB")); break; case MouseMessages.WM_RBUTTONDOWN: KeyDown?.Invoke(this, new HookEventArgs("Mouse RB")); break; case MouseMessages.WM_RBUTTONUP: KeyUp?.Invoke(this, new HookEventArgs("Mouse RB")); break; case MouseMessages.WM_MBUTTONDOWN: KeyDown?.Invoke(this, new HookEventArgs("Mouse MB")); break; case MouseMessages.WM_MBUTTONUP: KeyUp?.Invoke(this, new HookEventArgs("Mouse MB")); break; case MouseMessages.WM_XBUTTONDOWN when lParam.hwnd.Equals((IntPtr)0x00010000): KeyDown?.Invoke(this, new HookEventArgs("Mouse XB1")); break; case MouseMessages.WM_XBUTTONUP when lParam.hwnd.Equals((IntPtr)0x00010000): KeyUp?.Invoke(this, new HookEventArgs("Mouse XB1")); break; case MouseMessages.WM_XBUTTONDOWN when lParam.hwnd.Equals((IntPtr)0x00020000): KeyDown?.Invoke(this, new HookEventArgs("Mouse XB2")); break; case MouseMessages.WM_XBUTTONUP when lParam.hwnd.Equals((IntPtr)0x00020000): KeyUp?.Invoke(this, new HookEventArgs("Mouse XB2")); break; case MouseMessages.WM_MOUSEWHEEL: { if (lParam.hwnd.Equals((IntPtr)(-7864320))) { KeyDown?.Invoke(this, new HookEventArgs("Wheel Down")); KeyUp?.Invoke(this, new HookEventArgs("Wheel Up")); } else if (lParam.hwnd.Equals((IntPtr)0x00780000)) { KeyDown?.Invoke(this, new HookEventArgs("Wheel Up")); KeyUp?.Invoke(this, new HookEventArgs("Wheel Down")); } WheelStopped(); break; } } return(CallNextHookEx(_hookHandle, code, wParam, ref lParam)); }
/// <summary> /// Hook process messages. /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> static IntPtr HookProcFunction(int nCode, IntPtr wParam, [In] IntPtr lParam) { if (nCode == 0) { if (localHook) { MOUSEHOOKSTRUCT mhs = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT)); #region switch switch (wParam.ToInt32()) { case WM_LBUTTONDOWN: if (MouseDown != null) { MouseDown(null, new MouseEventArgs(MouseButtons.Left, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_LBUTTONUP: if (MouseUp != null) { MouseUp(null, new MouseEventArgs(MouseButtons.Left, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MBUTTONDOWN: if (MouseDown != null) { MouseDown(null, new MouseEventArgs(MouseButtons.Middle, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MBUTTONUP: if (MouseUp != null) { MouseUp(null, new MouseEventArgs(MouseButtons.Middle, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MOUSEMOVE: if (MouseMove != null) { MouseMove(null, new MouseEventArgs(MouseButtons.None, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MOUSEWHEEL: // Данный хук не позволяет узнать куда вращается колесо мыши. break; case WM_RBUTTONDOWN: if (MouseDown != null) { MouseDown(null, new MouseEventArgs(MouseButtons.Right, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_RBUTTONUP: if (MouseUp != null) { MouseUp(null, new MouseEventArgs(MouseButtons.Right, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; default: //Debug.WriteLine(string.Format("X:{0}; Y:{1}; Handle:{2}; HitTest:{3}; EI:{4}; wParam:{5}; lParam:{6}", // mhs.pt.X, mhs.pt.Y, mhs.hwnd, mhs.wHitTestCode, mhs.dwExtraInfo, wParam.ToString(), lParam.ToString())); break; } #endregion } else { MSLLHOOKSTRUCT mhs = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); #region switch switch (wParam.ToInt32()) { case WM_LBUTTONDOWN: if (MouseDown != null) { MouseDown(null, new MouseEventArgs(MouseButtons.Left, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_LBUTTONUP: if (MouseUp != null) { MouseUp(null, new MouseEventArgs(MouseButtons.Left, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MBUTTONDOWN: if (MouseDown != null) { MouseDown(null, new MouseEventArgs(MouseButtons.Middle, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MBUTTONUP: if (MouseUp != null) { MouseUp(null, new MouseEventArgs(MouseButtons.Middle, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MOUSEMOVE: if (MouseMove != null) { MouseMove(null, new MouseEventArgs(MouseButtons.None, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_MOUSEWHEEL: if (MouseMove != null) { MouseMove(null, new MouseEventArgs(MouseButtons.None, mhs.time, mhs.pt.X, mhs.pt.Y, mhs.mouseData >> 16)); } //Debug.WriteLine(string.Format("X:{0}; Y:{1}; MD:{2}; Time:{3}; EI:{4}; wParam:{5}; lParam:{6}", // mhs.pt.X, mhs.pt.Y, mhs.mouseData, mhs.time, mhs.dwExtraInfo, wParam.ToString(), lParam.ToString())); break; case WM_RBUTTONDOWN: if (MouseDown != null) { MouseDown(null, new MouseEventArgs(MouseButtons.Right, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; case WM_RBUTTONUP: if (MouseUp != null) { MouseUp(null, new MouseEventArgs(MouseButtons.Right, 1, mhs.pt.X, mhs.pt.Y, 0)); } break; default: break; } #endregion } } return(API.CallNextHookEx(hHook, nCode, wParam, lParam)); }