Пример #1
0
        private static void RaiseMouseMoveEvent(MOUSELLHOOKSTRUCT mouseHookStruct, ChoMouseEventArgs e)
        {
            EventHandler <ChoMouseEventArgs> mouseMove = _mouseMove;

            if (mouseMove == null)
            {
                return;
            }

            //If someone listens to move and there was a change in coordinates raise move event
            if (_oldMouseX != mouseHookStruct.Point.X || _oldMouseY != mouseHookStruct.Point.Y)
            {
                _oldMouseX = mouseHookStruct.Point.X;
                _oldMouseY = mouseHookStruct.Point.Y;
                mouseMove.Raise(null, e);
            }
        }
Пример #2
0
        protected override bool HooksCallbackProc(int code, int wParam, IntPtr lParam)
        {
            if (code > -1)
            {
                //Marshall the data from callback.
                MOUSELLHOOKSTRUCT mouseHookStruct = (MOUSELLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSELLHOOKSTRUCT));

                MouseButtons      button    = GetButton(wParam);
                ChoMouseEventType eventType = GetEventType(wParam);

                ChoMouseEventArgs e = new ChoMouseEventArgs(
                    button,
                    (eventType == ChoMouseEventType.DoubleClick ? 2 : 1),
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    (eventType == ChoMouseEventType.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 = ChoMouseEventType.None;
                }

                switch (eventType)
                {
                case ChoMouseEventType.MouseDown:
                    _mouseDown.Raise(this, e);
                    break;

                case ChoMouseEventType.MouseUp:
                    _mouseClick.Raise(this, e);
                    _mouseUp.Raise(this, e);
                    break;

                case ChoMouseEventType.DoubleClick:
                    _mouseDblClick.Raise(this, e);
                    break;

                case ChoMouseEventType.MouseWheel:
                    _mouseWheel.Raise(this, e);
                    break;

                case ChoMouseEventType.MouseMove:
                    //If someone listens to move and there was a change in coordinates raise move event
                    if (_oldMouseX != mouseHookStruct.Point.X || _oldMouseY != mouseHookStruct.Point.Y)
                    {
                        _oldMouseX = mouseHookStruct.Point.X;
                        _oldMouseY = mouseHookStruct.Point.Y;
                        _mouseMove.Raise(this, e);
                    }
                    break;

                default:
                    break;
                }

                return(e.Handled);
            }

            return(false);
        }
Пример #3
0
        private static bool MouseHooksProc(int code, int wParam, IntPtr lParam)
        {
            bool handled = false;

            if (code >= 0)
            {
                //Marshall the data from callback.
                MOUSELLHOOKSTRUCT mouseHookStruct = (MOUSELLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSELLHOOKSTRUCT));

                //detect button clicked
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                int          clickCount = 0;
                bool         mouseDown  = false;
                bool         mouseUp    = false;

                switch (wParam)
                {
                case ChoUser32.WM_LBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case ChoUser32.WM_LBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case ChoUser32.WM_LBUTTONDBLCLK:
                    button     = MouseButtons.Left;
                    clickCount = 2;
                    break;

                case ChoUser32.WM_RBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case ChoUser32.WM_RBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case ChoUser32.WM_RBUTTONDBLCLK:
                    button     = MouseButtons.Right;
                    clickCount = 2;
                    break;

                case ChoUser32.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);

                    //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
                ChoMouseEventArgs e = new ChoMouseEventArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta);

                //Mouse up
                if (mouseUp)
                {
                    _mouseUp.Raise(null, e);
                }

                handled = handled || e.Handled;

                //Mouse down
                if (mouseDown)
                {
                    _mouseDown.Raise(null, e);
                }

                handled = handled || e.Handled;

                //If someone listens to click and a click is heppened
                if (clickCount > 0)
                {
                    _mouseClick.Raise(null, e);
                }

                handled = handled || e.Handled;

                //If someone listens to double click and a click is heppened
                if (clickCount == 2)
                {
                    _mouseDblClick.Raise(null, e);
                }

                handled = handled || e.Handled;

                //Wheel was moved
                if (mouseDelta != 0)
                {
                    _mouseWheel.Raise(null, e);
                }

                handled = handled || e.Handled;

                RaiseMouseMoveEvent(mouseHookStruct, e);

                handled = handled || e.Handled;
            }

            return(handled);
        }