private void MouseWheelHendler(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
            Point    mousePosition        = e.GetPosition(_mapV);
            Location polygonPointLocation = _mapV.ViewportPointToLocation(mousePosition);

            MouseWheelEvent?.Invoke(e.Delta, polygonPointLocation);
        }
        /// <summary>
        /// Update mouse-related events.
        /// Call this function at the begining of every Update() frame.
        /// </summary>
        public void Update()
        {
            // store previous state and get new state
            _prevState = _newState;
            _newState  = Mouse.GetState();

            // get new mouse position
            _mousePos = _newState.Position;

            // generate mouse-button events
            foreach (MouseButtons button in System.Enum.GetValues(typeof(MouseButtons)))
            {
                // mouse button release event
                if (ReleasedThisFrame(button))
                {
                    OnMouseButtonReleased?.Invoke(button);
                }

                // mouse button pressed event
                if (PressedThisFrame(button))
                {
                    OnMouseButtonPressed?.Invoke(button);
                }
            }

            // is mouse moving this frame?
            IsMoving = PositionDelta != Point.Zero;

            // generate mouse-move events
            if (IsMoving)
            {
                OnMouseMove?.Invoke(PositionDelta);
            }

            // gerate scroll wheel events
            if (ScrollWheelDelta != 0)
            {
                OnScrollWheelUse?.Invoke(ScrollWheelDelta);
            }
        }
Exemple #3
0
        protected override IntPtr CustomHookProc(IntPtr wParam, IntPtr lParam)
        {
            MSLLHOOKSTRUCT ms =
                (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
            bool injected = (ms.flags & (uint)LLMHF.INJECTED) != 0;

            switch ((WM)wParam)
            {
            case WM.LBUTTONDOWN:
                if (MouseDownEvent?.Invoke(this, (uint)VK.LBUTTON, ms.pt.x, ms.pt.y, 0, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.LBUTTONUP:
                if (MouseUpEvent?.Invoke(this, (uint)VK.LBUTTON, ms.pt.x, ms.pt.y, 0, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.MOUSEMOVE:
                if (MouseMoveEvent?.Invoke(this, 0, ms.pt.x, ms.pt.y, 0, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.MOUSEWHEEL:
                if (MouseWheelEvent?.Invoke(this, 0, ms.pt.x, ms.pt.y, HighWord(ms.mouseData), injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.MOUSEHWHEEL:
                if (MouseHWheelEvent?.Invoke(this, 0, ms.pt.x, ms.pt.y, HighWord(ms.mouseData), injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.RBUTTONDOWN:
                if (MouseDownEvent?.Invoke(this, (uint)VK.RBUTTON, ms.pt.x, ms.pt.y, 0, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.RBUTTONUP:
                if (MouseUpEvent?.Invoke(this, (uint)VK.RBUTTON, ms.pt.x, ms.pt.y, 0, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.MBUTTONDOWN:
                if (MouseDownEvent?.Invoke(this, (uint)VK.MBUTTON, ms.pt.x, ms.pt.y, 0, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.MBUTTONUP:
                if (MouseUpEvent?.Invoke(this, (uint)VK.MBUTTON, ms.pt.x, ms.pt.y, 0, injected) == true)
                {
                    return((IntPtr)(-1));
                }
                break;

            case WM.XBUTTONDOWN:
                if (HighWord(ms.mouseData) == (int)MOUSEDATA.XBUTTON1)
                {
                    if (MouseDownEvent?.Invoke(this, (uint)VK.XBUTTON1, ms.pt.x, ms.pt.y, 0, injected) == true)
                    {
                        return((IntPtr)(-1));
                    }
                }
                else if (HighWord(ms.mouseData) == (int)MOUSEDATA.XBUTTON2)
                {
                    if (MouseDownEvent?.Invoke(this, (uint)VK.XBUTTON2, ms.pt.x, ms.pt.y, 0, injected) == true)
                    {
                        return((IntPtr)(-1));
                    }
                }
                break;

            case WM.XBUTTONUP:
                if (HighWord(ms.mouseData) == (int)MOUSEDATA.XBUTTON1)
                {
                    if (MouseUpEvent?.Invoke(this, (uint)VK.XBUTTON1, ms.pt.x, ms.pt.y, 0, injected) == true)
                    {
                        return((IntPtr)(-1));
                    }
                }
                else if (HighWord(ms.mouseData) == (int)MOUSEDATA.XBUTTON2)
                {
                    if (MouseUpEvent?.Invoke(this, (uint)VK.XBUTTON2, ms.pt.x, ms.pt.y, 0, injected) == true)
                    {
                        return((IntPtr)(-1));
                    }
                }
                break;
            }
            return(IntPtr.Zero);
        }
        public override bool Poll()
        {
            bool returnStatus = false;



            MouseStateEventData[] eventDataArray;

            lock (_mouseEventData)
            {
                eventDataArray = _mouseEventData.ToArray();
                _mouseEventData.Clear();
            }

            if (eventDataArray.Length == 0)
            {
                return(returnStatus);
            }


            MouseButtons[] currentState = _getButtonState(eventDataArray);

            Point currentPoint = eventDataArray[0].Data.Position;


            MouseButtons[] pressedButtons  = GetNewItems(_mouseStateArray, currentState);
            MouseButtons[] releasedButtons = GetMissingItems(_mouseStateArray, currentState);

            //Subtract current values so positive values result in a positive delta.
            Point relativePoint = currentPoint - _mouseStatePoint;
            int   wheelDelta    = eventDataArray[0].Data.WheelDelta;

            _mouseStateWheel = _mouseStateWheel + wheelDelta;


            MouseEventData eventData = new MouseEventData(pressedButtons, releasedButtons, currentState, currentPoint, relativePoint, _mouseStateWheel, wheelDelta);

            //System.Diagnostics.Trace.WriteLine(eventData.ToString());

            if (pressedButtons.Length != 0 || releasedButtons.Length != 0 || relativePoint != Point.Empty || wheelDelta != 0)
            {
                MouseEvent?.Invoke(new MouseEventArgs(eventData));
                returnStatus = true;
            }

            if (pressedButtons.Length != 0)
            {
                MouseDownEvent?.Invoke(new MouseEventArgs(eventData));
            }

            if (releasedButtons.Length != 0)
            {
                MouseUpEvent?.Invoke(new MouseEventArgs(eventData));
            }


            if (relativePoint != Point.Empty)
            {
                MouseMoveEvent?.Invoke(new MouseEventArgs(eventData));
            }

            if (wheelDelta != 0)
            {
                MouseWheelEvent?.Invoke(new MouseEventArgs(eventData));
            }

            _mouseStateArray = currentState;
            return(returnStatus);
        }
 private void OnMouseWheel(int wheel) => MouseWheelEvent?.Invoke(this, wheel, DateTime.Now);
        private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var   mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
                var   button          = MouseButtons.None;
                short mouseDelta      = 0;
                var   clickCount      = 0;
                var   mouseDown       = false;
                var   mouseUp         = false;
                switch ((WindowsMessage)wParam)
                {
                case WindowsMessage.WM_LBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

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

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

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

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

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

                case WindowsMessage.WM_MOUSEWHEEL:
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);
                    break;
                }
                var e = new MouseEventExtArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta);
                if (MouseEvent != null)
                {
                    MouseEvent.Invoke(null, e);
                }
                if (MouseUpEvent != null && mouseUp)
                {
                    MouseUpEvent.Invoke(null, e);
                }
                if (MouseDownEvent != null && mouseDown)
                {
                    MouseDownEvent.Invoke(null, e);
                }
                if (MouseClickEvent != null && clickCount > 0)
                {
                    MouseClickEvent.Invoke(null, e);
                }
                if (MouseDoubleClickEvent != null && clickCount == 2)//这里不会触发
                {
                    MouseDoubleClickEvent.Invoke(null, e);
                }
                if (MouseWheelEvent != null && mouseDelta != 0)
                {
                    MouseWheelEvent.Invoke(null, e);
                }
                if (MouseMoveEvent != null && (oldX != mouseHookStruct.Point.X || oldY != mouseHookStruct.Point.Y))
                {
                    oldX = mouseHookStruct.Point.X;
                    oldY = mouseHookStruct.Point.Y;
                    if (MouseMoveEvent != null)
                    {
                        MouseMoveEvent.Invoke(null, e);
                    }
                }
                if (e.Handled)
                {
                    return(-1);
                }
            }
            return(NativeMethods.CallNextHookEx(idHook, nCode, wParam, lParam));
        }