public static bool TrackMouseEvent(NativeMethods.TRACKMOUSEEVENT tme)
        {
            bool retVal   = SafeNativeMethodsPrivate.TrackMouseEvent(tme);
            int  win32Err = Marshal.GetLastWin32Error(); // Dance around FxCop

            if (!retVal && win32Err != 0)
            {
                throw new System.ComponentModel.Win32Exception(win32Err);
            }
            return(retVal);
        }
Exemple #2
0
        private void HookMouseEvent()
        {
            if (!_trakingMouse)
            {
                _trakingMouse = true;
                if (this._trackMouseEvent == null)
                {
                    this._trackMouseEvent         = new NativeMethods.TRACKMOUSEEVENT();
                    this._trackMouseEvent.dwFlags =
                        (int)(NativeMethods.TrackMouseEventFalgs.TME_HOVER |
                              NativeMethods.TrackMouseEventFalgs.TME_LEAVE |
                              NativeMethods.TrackMouseEventFalgs.TME_NONCLIENT);

                    this._trackMouseEvent.hwndTrack = this.Handle;
                }

                if (NativeMethods.TrackMouseEvent(this._trackMouseEvent) == false)
                {
                    // use getlasterror to see whats wrong
                    Log(MethodInfo.GetCurrentMethod(), "Failed enabling TrackMouseEvent: error {0}",
                        NativeMethods.GetLastError());
                }
            }
        }
        protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
            case NativeMethods.WM_MOUSEWHEEL:
                if (_mouseInWindow)
                {
                    int delta = NativeMethods.GetWheelDeltaWParam(wParam.ToInt32());
                    RaiseHwndMouseWheel(new HwndMouseEventArgs(_mouseState, delta, 0));
                }
                break;

            case NativeMethods.WM_LBUTTONDOWN:
                _mouseState.LeftButton = MouseButtonState.Pressed;
                RaiseHwndLButtonDown(new HwndMouseEventArgs(_mouseState));
                break;

            case NativeMethods.WM_LBUTTONUP:
                _mouseState.LeftButton = MouseButtonState.Released;
                RaiseHwndLButtonUp(new HwndMouseEventArgs(_mouseState));
                break;

            case NativeMethods.WM_LBUTTONDBLCLK:
                RaiseHwndLButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.Left));
                break;

            case NativeMethods.WM_RBUTTONDOWN:
                _mouseState.RightButton = MouseButtonState.Pressed;
                RaiseHwndRButtonDown(new HwndMouseEventArgs(_mouseState));
                break;

            case NativeMethods.WM_RBUTTONUP:
                _mouseState.RightButton = MouseButtonState.Released;
                RaiseHwndRButtonUp(new HwndMouseEventArgs(_mouseState));
                break;

            case NativeMethods.WM_RBUTTONDBLCLK:
                RaiseHwndRButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.Right));
                break;

            case NativeMethods.WM_MBUTTONDOWN:
                _mouseState.MiddleButton = MouseButtonState.Pressed;
                RaiseHwndMButtonDown(new HwndMouseEventArgs(_mouseState));
                break;

            case NativeMethods.WM_MBUTTONUP:
                _mouseState.MiddleButton = MouseButtonState.Released;
                RaiseHwndMButtonUp(new HwndMouseEventArgs(_mouseState));
                break;

            case NativeMethods.WM_MBUTTONDBLCLK:
                RaiseHwndMButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.Middle));
                break;

            case NativeMethods.WM_XBUTTONDOWN:
                if (((int)wParam & NativeMethods.MK_XBUTTON1) != 0)
                {
                    _mouseState.X1Button = MouseButtonState.Pressed;
                    RaiseHwndX1ButtonDown(new HwndMouseEventArgs(_mouseState));
                }
                else if (((int)wParam & NativeMethods.MK_XBUTTON2) != 0)
                {
                    _mouseState.X2Button = MouseButtonState.Pressed;
                    RaiseHwndX2ButtonDown(new HwndMouseEventArgs(_mouseState));
                }
                break;

            case NativeMethods.WM_XBUTTONUP:
                if (((int)wParam & NativeMethods.MK_XBUTTON1) != 0)
                {
                    _mouseState.X1Button = MouseButtonState.Released;
                    RaiseHwndX1ButtonUp(new HwndMouseEventArgs(_mouseState));
                }
                else if (((int)wParam & NativeMethods.MK_XBUTTON2) != 0)
                {
                    _mouseState.X2Button = MouseButtonState.Released;
                    RaiseHwndX2ButtonUp(new HwndMouseEventArgs(_mouseState));
                }
                break;

            case NativeMethods.WM_XBUTTONDBLCLK:
                if (((int)wParam & NativeMethods.MK_XBUTTON1) != 0)
                {
                    RaiseHwndX1ButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.XButton1));
                }
                else if (((int)wParam & NativeMethods.MK_XBUTTON2) != 0)
                {
                    RaiseHwndX2ButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.XButton2));
                }
                break;

            case NativeMethods.WM_MOUSEMOVE:
                // If the application isn't in focus, we don't handle this message
                if (!_applicationHasFocus)
                {
                    break;
                }

                // record the prevous and new position of the mouse
                _mouseState.ScreenPosition = PointToScreen(new Point(
                                                               NativeMethods.GetXLParam((int)lParam),
                                                               NativeMethods.GetYLParam((int)lParam)));

                if (!_mouseInWindow)
                {
                    _mouseInWindow = true;

                    RaiseHwndMouseEnter(new HwndMouseEventArgs(_mouseState));

                    // Track the previously focused window, and set focus to this window.
                    _hWndPrev = NativeMethods.GetFocus();
                    NativeMethods.SetFocus(_hWnd);

                    // send the track mouse event so that we get the WM_MOUSELEAVE message
                    var tme = new NativeMethods.TRACKMOUSEEVENT
                    {
                        cbSize  = Marshal.SizeOf(typeof(NativeMethods.TRACKMOUSEEVENT)),
                        dwFlags = NativeMethods.TME_LEAVE,
                        hWnd    = hwnd
                    };
                    NativeMethods.TrackMouseEvent(ref tme);
                }

                if (_mouseState.ScreenPosition != _previousPosition)
                {
                    RaiseHwndMouseMove(new HwndMouseEventArgs(_mouseState));
                }

                _previousPosition = _mouseState.ScreenPosition;

                break;

            case NativeMethods.WM_MOUSELEAVE:

                // If we have capture, we ignore this message because we're just
                // going to reset the cursor position back into the window
                if (_isMouseCaptured)
                {
                    break;
                }

                // Reset the state which releases all buttons and
                // marks the mouse as not being in the window.
                ResetMouseState();

                RaiseHwndMouseLeave(new HwndMouseEventArgs(_mouseState));

                NativeMethods.SetFocus(_hWndPrev);

                break;
            }

            return(base.WndProc(hwnd, msg, wParam, lParam, ref handled));
        }
		private void HookMouseEvent()
		{
			if (!_trakingMouse)
			{
				_trakingMouse = true;
				if (this._trackMouseEvent == null)
				{
					this._trackMouseEvent = new NativeMethods.TRACKMOUSEEVENT();
					this._trackMouseEvent.dwFlags =
							(int)(NativeMethods.TrackMouseEventFalgs.TME_HOVER |
										NativeMethods.TrackMouseEventFalgs.TME_LEAVE |
										NativeMethods.TrackMouseEventFalgs.TME_NONCLIENT);

					this._trackMouseEvent.hwndTrack = this.Handle;
				}

				if (NativeMethods.TrackMouseEvent(this._trackMouseEvent) == false)
					// use getlasterror to see whats wrong
					Log(MethodInfo.GetCurrentMethod(), "Failed enabling TrackMouseEvent: error {0}",
							NativeMethods.GetLastError());
			}
		}
 public static bool TrackMouseEvent(NativeMethods.TRACKMOUSEEVENT tme)
 {
     // only on NT - not on 95 - comctl32 has a wrapper for 95 and NT.
     return(_TrackMouseEvent(tme));
 }
 private static extern bool _TrackMouseEvent(NativeMethods.TRACKMOUSEEVENT tme);
 public static extern bool TrackMouseEvent(NativeMethods.TRACKMOUSEEVENT tme);
 public static bool TrackMouseEvent(NativeMethods.TRACKMOUSEEVENT tme)
 {
     return(_TrackMouseEvent(tme));
 }
        protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                case NativeMethods.WM_MOUSEWHEEL:
                    if (_mouseInWindow)
                    {
                        int delta = NativeMethods.GetWheelDeltaWParam(wParam.ToInt32());
                        RaiseHwndMouseWheel(new HwndMouseEventArgs(_mouseState, delta, 0));
                    }
                    break;
                case NativeMethods.WM_LBUTTONDOWN:
                    _mouseState.LeftButton = MouseButtonState.Pressed;
                    RaiseHwndLButtonDown(new HwndMouseEventArgs(_mouseState));
                    break;
                case NativeMethods.WM_LBUTTONUP:
                    _mouseState.LeftButton = MouseButtonState.Released;
                    RaiseHwndLButtonUp(new HwndMouseEventArgs(_mouseState));
                    break;
                case NativeMethods.WM_LBUTTONDBLCLK:
                    RaiseHwndLButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.Left));
                    break;
                case NativeMethods.WM_RBUTTONDOWN:
                    _mouseState.RightButton = MouseButtonState.Pressed;
                    RaiseHwndRButtonDown(new HwndMouseEventArgs(_mouseState));
                    break;
                case NativeMethods.WM_RBUTTONUP:
                    _mouseState.RightButton = MouseButtonState.Released;
                    RaiseHwndRButtonUp(new HwndMouseEventArgs(_mouseState));
                    break;
                case NativeMethods.WM_RBUTTONDBLCLK:
                    RaiseHwndRButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.Right));
                    break;
                case NativeMethods.WM_MBUTTONDOWN:
                    _mouseState.MiddleButton = MouseButtonState.Pressed;
                    RaiseHwndMButtonDown(new HwndMouseEventArgs(_mouseState));
                    break;
                case NativeMethods.WM_MBUTTONUP:
                    _mouseState.MiddleButton = MouseButtonState.Released;
                    RaiseHwndMButtonUp(new HwndMouseEventArgs(_mouseState));
                    break;
                case NativeMethods.WM_MBUTTONDBLCLK:
                    RaiseHwndMButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.Middle));
                    break;
                case NativeMethods.WM_XBUTTONDOWN:
                    if (((int) wParam & NativeMethods.MK_XBUTTON1) != 0)
                    {
                        _mouseState.X1Button = MouseButtonState.Pressed;
                        RaiseHwndX1ButtonDown(new HwndMouseEventArgs(_mouseState));
                    }
                    else if (((int) wParam & NativeMethods.MK_XBUTTON2) != 0)
                    {
                        _mouseState.X2Button = MouseButtonState.Pressed;
                        RaiseHwndX2ButtonDown(new HwndMouseEventArgs(_mouseState));
                    }
                    break;
                case NativeMethods.WM_XBUTTONUP:
                    if (((int) wParam & NativeMethods.MK_XBUTTON1) != 0)
                    {
                        _mouseState.X1Button = MouseButtonState.Released;
                        RaiseHwndX1ButtonUp(new HwndMouseEventArgs(_mouseState));
                    }
                    else if (((int) wParam & NativeMethods.MK_XBUTTON2) != 0)
                    {
                        _mouseState.X2Button = MouseButtonState.Released;
                        RaiseHwndX2ButtonUp(new HwndMouseEventArgs(_mouseState));
                    }
                    break;
                case NativeMethods.WM_XBUTTONDBLCLK:
                    if (((int) wParam & NativeMethods.MK_XBUTTON1) != 0)
                        RaiseHwndX1ButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.XButton1));
                    else if (((int) wParam & NativeMethods.MK_XBUTTON2) != 0)
                        RaiseHwndX2ButtonDblClick(new HwndMouseEventArgs(_mouseState, MouseButton.XButton2));
                    break;
                case NativeMethods.WM_MOUSEMOVE:
                    // If the application isn't in focus, we don't handle this message
                    if (!_applicationHasFocus)
                        break;

                    // record the prevous and new position of the mouse
                    _mouseState.ScreenPosition = PointToScreen(new Point(
                        NativeMethods.GetXLParam((int) lParam),
                        NativeMethods.GetYLParam((int) lParam)));

                    if (!_mouseInWindow)
                    {
                        _mouseInWindow = true;

                        RaiseHwndMouseEnter(new HwndMouseEventArgs(_mouseState));

                        // Track the previously focused window, and set focus to this window.
                        _hWndPrev = NativeMethods.GetFocus();
                        NativeMethods.SetFocus(_hWnd);

                        // send the track mouse event so that we get the WM_MOUSELEAVE message
                        var tme = new NativeMethods.TRACKMOUSEEVENT
                        {
                            cbSize = Marshal.SizeOf(typeof (NativeMethods.TRACKMOUSEEVENT)),
                            dwFlags = NativeMethods.TME_LEAVE,
                            hWnd = hwnd
                        };
                        NativeMethods.TrackMouseEvent(ref tme);
                    }

                    if (_mouseState.ScreenPosition != _previousPosition)
                        RaiseHwndMouseMove(new HwndMouseEventArgs(_mouseState));

                    _previousPosition = _mouseState.ScreenPosition;

                    break;
                case NativeMethods.WM_MOUSELEAVE:

                    // If we have capture, we ignore this message because we're just
                    // going to reset the cursor position back into the window
                    if (_isMouseCaptured)
                        break;

                    // Reset the state which releases all buttons and
                    // marks the mouse as not being in the window.
                    ResetMouseState();

                    RaiseHwndMouseLeave(new HwndMouseEventArgs(_mouseState));

                    NativeMethods.SetFocus(_hWndPrev);

                    break;
            }

            return base.WndProc(hwnd, msg, wParam, lParam, ref handled);
        }