private static IntPtr HookProc(int nCode, NativeMethods.Message wParam, ref NativeMethods.MSLLHOOKSTRUCT lParam) { if (nCode >= 0 && wParam != NativeMethods.Message.WM_MOUSEMOVE) { Stop(); if (wParam == NativeMethods.Message.WM_LBUTTONDOWN) { Picked?.Invoke(PickWindowTitle(lParam.pt)); return((IntPtr)1); } } return(NativeMethods.CallNextHookEx(_hHook, nCode, wParam, ref lParam)); }
/// <summary> /// フックチェーンにインストールするフックプロシージャ /// </summary> /// <param name="nCode">フックプロシージャに渡すフックコード</param> /// <param name="msg">フックプロシージャに渡す値</param> /// <param name="msllhookstruct">フックプロシージャに渡す値</param> /// <returns>フックチェーン内の次のフックプロシージャの戻り値</returns> private static System.IntPtr HookProcedure(int nCode, NativeMethods.Message msg, ref NativeMethods.MSLLHOOKSTRUCT s) { if (nCode >= 0 && HookEvent != null) { State.button = GetButtonState(msg, ref s); State.posX = s.pt.x; State.posY = s.pt.y; State.mouseData = s.mouseData; State.flags = s.flags; State.time = s.time; State.dwExtraInfo = s.dwExtraInfo; HookEvent(ref State); if (IsCancel) { IsCancel = false; return((System.IntPtr) 1); } } return(NativeMethods.CallNextHookEx(Handle, nCode, msg, ref s)); }
/// <summary> /// 操作されたボタンを取得します。 /// </summary> /// <param name="msg">マウスに関するウィンドウメッセージ</param> /// <param name="s">低レベルのマウスの入力イベントの構造体</param> /// <returns></returns> private static ButtonType GetButtonState(NativeMethods.Message msg, ref NativeMethods.MSLLHOOKSTRUCT s) { switch (msg) { case NativeMethods.Message.WM_MOUSEMOVE: return(ButtonType.MOVE); case NativeMethods.Message.WM_LBUTTONDOWN: return(ButtonType.L_DOWN); case NativeMethods.Message.WM_LBUTTONUP: return(ButtonType.L_UP); case NativeMethods.Message.WM_LBUTTONDBLCLK: return(ButtonType.UNKNOWN); case NativeMethods.Message.WM_RBUTTONDOWN: return(ButtonType.R_DOWN); case NativeMethods.Message.WM_RBUTTONUP: return(ButtonType.R_UP); case NativeMethods.Message.WM_MBUTTONDOWN: return(ButtonType.M_DOWN); case NativeMethods.Message.WM_MBUTTONUP: return(ButtonType.M_UP); case NativeMethods.Message.WM_MOUSEWHEEL: return(((short)((s.mouseData >> 16) & 0xffff) > 0) ? ButtonType.W_UP : ButtonType.W_DOWN); case NativeMethods.Message.WM_XBUTTONDOWN: switch (s.mouseData >> 16) { case 1: return(ButtonType.X1_DOWN); case 2: return(ButtonType.X2_DOWN); default: return(ButtonType.UNKNOWN); } case NativeMethods.Message.WM_XBUTTONUP: switch (s.mouseData >> 16) { case 1: return(ButtonType.X1_UP); case 2: return(ButtonType.X2_UP); default: return(ButtonType.UNKNOWN); } default: return(ButtonType.UNKNOWN); } }
/// <summary> /// Occurs when the callback is received for an event. /// </summary> /// <param name="nCode">The hook code.</param> /// <param name="wParam">A parameter containing event data.</param> /// <param name="lParam">A parameter containing event data.</param> /// <returns><b>true</b> if the event was handled, otherwise <b>false</b>.</returns> protected override bool OnCallback(int nCode, IntPtr wParam, IntPtr lParam) { bool handled = false; ////Debug.WriteLine("MouseHook::OnCallback - {0}:{1}:{2}", nCode, wParam, lParam); switch (nCode) { case NativeMethods.HC_ACTION: NativeMethods.MSLLHOOKSTRUCT info = (NativeMethods.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(NativeMethods.MSLLHOOKSTRUCT)); DateTime dt = DateTime.Today.AddMilliseconds(info.time); ////Debug.WriteLine("dt: {0}", dt); ////Debug.WriteLine("pt: {0}", info.pt); ////Debug.WriteLine("mouseData: {0}", info.mouseData); ////Debug.WriteLine("flags: {0}", info.flags); ////Debug.WriteLine("time: {0}", info.time); ////Debug.WriteLine("dwExtraInfo: {0}", info.dwExtraInfo); ////Debug.WriteLine(""); int identifier = wParam.ToInt32(); switch (identifier) { case NativeMethods.WM_MOUSEMOVE: this.OnMouseMove(new MouseMoveEventArgs() { X = info.pt.x, Y = info.pt.y, Timestamp = dt }); break; case NativeMethods.WM_MOUSEWHEEL: case NativeMethods.WM_MOUSEHWHEEL: this.OnMouseWheel(EventArgs.Empty); break; case NativeMethods.WM_LBUTTONUP: case NativeMethods.WM_MBUTTONUP: case NativeMethods.WM_RBUTTONUP: case NativeMethods.WM_XBUTTONUP: case NativeMethods.WM_NCXBUTTONUP: this.OnMouseUp(new MouseButtonEventArgs() { ButtonState = MouseButtonStateEnum.Released, Button = ConvertToButtonEnum(identifier), X = info.pt.x, Y = info.pt.y, Timestamp = dt }); break; case NativeMethods.WM_LBUTTONDOWN: case NativeMethods.WM_MBUTTONDOWN: case NativeMethods.WM_RBUTTONDOWN: case NativeMethods.WM_XBUTTONDOWN: case NativeMethods.WM_NCXBUTTONDOWN: this.OnMouseDown(new MouseButtonEventArgs() { ButtonState = MouseButtonStateEnum.Released, Button = ConvertToButtonEnum(identifier), X = info.pt.x, Y = info.pt.y, Timestamp = dt }); break; } break; } return(handled); }