/// <summary> /// A callback function which will be called every Time a mouse activity detected. /// </summary> /// <param name="nCode"> /// [in] Specifies whether the hook procedure must process the message. /// If nCode is HC_ACTION, the hook procedure must process the message. /// If nCode is less than zero, the hook procedure must pass the message to the /// CallNextHookEx function without further processing and must return the /// value returned by CallNextHookEx. /// </param> /// <param name="wParam"> /// [in] Specifies whether the message was sent by the current thread. /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. /// </param> /// <param name="lParam"> /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. /// </param> /// <returns> /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook /// procedure does not call CallNextHookEx, the return value should be zero. /// </returns> private IntPtr MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //Marshall the data from callback. WinAPI.MouseLLHookStruct mouseHookStruct = (WinAPI.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(WinAPI.MouseLLHookStruct)); MouseButtons button = MouseButtons.None; short mouseDelta = 0; int clickCount = 0; MouseEventExtArgs.UpDown upDown = MouseEventExtArgs.UpDown.None; bool mouseUp = false; //detect button clicked switch ((WinAPI.WindowMessage)wParam) { case WinAPI.WindowMessage.WM_LBUTTONDOWN: upDown = MouseEventExtArgs.UpDown.Down; button = MouseButtons.Left; clickCount = 1; break; case WinAPI.WindowMessage.WM_LBUTTONUP: upDown = MouseEventExtArgs.UpDown.Up; button = MouseButtons.Left; clickCount = 1; break; case WinAPI.WindowMessage.WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case WinAPI.WindowMessage.WM_RBUTTONDOWN: upDown = MouseEventExtArgs.UpDown.Down; button = MouseButtons.Right; clickCount = 1; break; case WinAPI.WindowMessage.WM_RBUTTONUP: upDown = MouseEventExtArgs.UpDown.Up; button = MouseButtons.Right; clickCount = 1; break; case WinAPI.WindowMessage.WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; case WinAPI.WindowMessage.WM_MOUSEWHEEL: //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta. //One wheel click is defined as WHEEL_DELTA, which is 120. //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); if (mouseDelta > 0) upDown = MouseEventExtArgs.UpDown.Up; if (mouseDelta < 0) upDown = MouseEventExtArgs.UpDown.Down; //TODO: X BUTTONS (I havent them so was unable to test) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, //and the low-order word is reserved. This value can be one or more of the following values. //Otherwise, MouseData is not used. break; } //generate event // if (CurrentMouseArgs != null) // // UpdateCount += 1; CurrentMouseArgs = new MouseEventExtArgs( button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta) { Wheel = mouseDelta != 0, UpOrDown = upDown }; MouseChanged = true; // Raise it OnMouseChangedEvent(this, CurrentMouseArgs); } //call next hook return WinAPI.CallNextHookEx(hMouseHook, nCode, wParam, lParam); }
public MouseHook() { CurrentMouseArgs = new MouseEventExtArgs(); }
void SharpDXTabControl_MouseClickEventUp(object sender, MouseEventExtArgs e) { if (e.Button != MouseButtons.Left) return; if (TabHeaders == null) return; Vector2 cursorPoint = (Vector2)e.PosOnForm - this.GetAbsoluteLocation(); RectangleF cursor = new RectangleF(cursorPoint.X, cursorPoint.Y, 1, 1); for(int i = 0; i < TabHeaders.Length; i++) { if (TabHeaders[i].Intersects(cursor)) { this.SelectedIndex = i; break; } } }
protected virtual void OnMouseChangedEvent(object sender, MouseEventExtArgs e) { if (MouseEvent != null) MouseEvent(sender, e); }
void SharpDXTrackbar_MouseMovedEvent(object sender, MouseEventExtArgs e) { if (e.Button != MouseButtons.Left) return; Vector2 size = this.GetSize(); Vector2 trackbarSize = new Vector2(size.X - TrackbarHeight, 0); Vector2 cursorPos = new Vector2(((Vector2)e.PosOnForm).X - trackbarLocation.X,((Vector2)e.PosOnForm).Y - trackbarLocation.Y); if (cursorPos.X >= 0 && cursorPos.X <= trackbarSize.X) { if (cursorPos.Y >= -TrackbarHeight && cursorPos.Y <= TrackbarHeight) { float percent = 1f / trackbarSize.X * cursorPos.X; float range = Math.Abs(this.Minimum - this.Maximum); float val = range * percent; this.Value = this.Minimum + val; } } }
void SharpDXTrackbar_MouseWheelEvent(object sender, MouseEventExtArgs e) { if (!e.Wheel) return; float percent = this.Value/this.Maximum; if (e.UpOrDown == MouseEventExtArgs.UpDown.Up) percent += 0.01f; if (e.UpOrDown == MouseEventExtArgs.UpDown.Down) percent -= 0.01f; float range = Math.Abs(this.Minimum - this.Maximum); float val = range * percent; this.Value = this.Minimum + val; }
void SharpDXWindow_MouseClickEventDown(object sender, MouseEventExtArgs e) { if (e.Button == MouseButtons.Left) mouseDown = true; }
void SharpDXCheckBox_MouseClickEventUp(object sender, MouseEventExtArgs e) { if (e.Button == MouseButtons.Left) this.Checked = !this.Checked; }
void SharpDXWindow_MouseMovedEvent(object sender, MouseEventExtArgs e) { if (mouseDown) { var lastpos = (Vector2)e.PosOnForm; Vector2 offset = new Vector2(lastpos.X - this.LastMousePos.X, lastpos.Y - this.LastMousePos.Y); this.X += offset.X; this.Y += offset.Y; } }
void SharpDXButtonKey_MouseClickEventUp(object sender, MouseEventExtArgs e) { if (e.Button != MouseButtons.Left) return; listen = true; skip = 10; }
/// <summary> /// A callback function which will be called every Time a mouse activity detected. /// </summary> /// <param name="nCode"> /// [in] Specifies whether the hook procedure must process the message. /// If nCode is HC_ACTION, the hook procedure must process the message. /// If nCode is less than zero, the hook procedure must pass the message to the /// CallNextHookEx function without further processing and must return the /// value returned by CallNextHookEx. /// </param> /// <param name="wParam"> /// [in] Specifies whether the message was sent by the current thread. /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. /// </param> /// <param name="lParam"> /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. /// </param> /// <returns> /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook /// procedure does not call CallNextHookEx, the return value should be zero. /// </returns> private IntPtr MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //Marshall the data from callback. WinAPI.MouseLLHookStruct mouseHookStruct = (WinAPI.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(WinAPI.MouseLLHookStruct)); MouseButtons button = MouseButtons.None; short mouseDelta = 0; int clickCount = 0; MouseEventExtArgs.UpDown upDown = MouseEventExtArgs.UpDown.None; bool mouseUp = false; //detect button clicked switch ((WinAPI.WindowMessage)wParam) { case WinAPI.WindowMessage.WM_LBUTTONDOWN: upDown = MouseEventExtArgs.UpDown.Down; button = MouseButtons.Left; clickCount = 1; break; case WinAPI.WindowMessage.WM_LBUTTONUP: upDown = MouseEventExtArgs.UpDown.Up; button = MouseButtons.Left; clickCount = 1; break; case WinAPI.WindowMessage.WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case WinAPI.WindowMessage.WM_RBUTTONDOWN: upDown = MouseEventExtArgs.UpDown.Down; button = MouseButtons.Right; clickCount = 1; break; case WinAPI.WindowMessage.WM_RBUTTONUP: upDown = MouseEventExtArgs.UpDown.Up; button = MouseButtons.Right; clickCount = 1; break; case WinAPI.WindowMessage.WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; case WinAPI.WindowMessage.WM_MOUSEWHEEL: //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta. //One wheel click is defined as WHEEL_DELTA, which is 120. //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); if (mouseDelta > 0) { upDown = MouseEventExtArgs.UpDown.Up; } if (mouseDelta < 0) { upDown = MouseEventExtArgs.UpDown.Down; } //TODO: X BUTTONS (I havent them so was unable to test) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, //and the low-order word is reserved. This value can be one or more of the following values. //Otherwise, MouseData is not used. break; } //generate event // if (CurrentMouseArgs != null) // // UpdateCount += 1; CurrentMouseArgs = new MouseEventExtArgs( button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta) { Wheel = mouseDelta != 0, UpOrDown = upDown }; MouseChanged = true; // Raise it OnMouseChangedEvent(this, CurrentMouseArgs); } //call next hook return(WinAPI.CallNextHookEx(hMouseHook, nCode, wParam, lParam)); }
void SharpDXRadioButton_MouseClickEventUp(object sender, MouseEventExtArgs e) { if (e.Button == MouseButtons.Left && !this.Checked) { this.Checked = true; } }