Custom Mouse Event Args
Наследование: System.EventArgs
Пример #1
0
        /// <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 int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            //If not ok and no one listens to our events, call next hook.
            if (nCode < 0 || OnMouseActivity == null)
                return CallNextHookEx(hMouseHook, nCode, wParam, lParam);

            //Marshall the data from callback.
            var mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

            //Detect button clicked
            var button = MouseButton.XButton1;
            short mouseDelta = 0;
            var state = MouseButtonState.Pressed;

            #region Switch Mouse Actions

            switch (wParam)
            {
                case WM_LBUTTONDOWN:
                    //case WM_LBUTTONUP: 
                    //case WM_LBUTTONDBLCLK: 
                    button = MouseButton.Left;
                    break;
                case WM_LBUTTONUP:
                    state = MouseButtonState.Released;
                    button = MouseButton.Left;
                    break;
                case WM_RBUTTONDOWN:
                    //case WM_RBUTTONUP: 
                    //case WM_RBUTTONDBLCLK: 
                    button = MouseButton.Right;
                    break;
                case 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. 
                    button = MouseButton.Middle;
                    break;
                case WM_MBUTTONDOWN:
                    //case WM_MBUTTONUP: 
                    //case WM_MBUTTONDBLCLK:
                    button = MouseButton.Middle;
                    break;
                //default: I can't return now, it will break the click detector.
                    //return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
                    //HU3HU3 - A little funny momment: I just frooze my cursor by returning 1 instead of calling the next hook. - Nicke
                    //Congrats to myself. ;D 
                    //05:24 AM 01/02/2014 (day-month-year)
            }

            #endregion

            //Double clicks
            int clickCount = 0;
            if (button != MouseButton.XButton1 || mouseDelta != 0)
                clickCount = (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) ? 2 : 1;

            //Generate event 
            var e = new CustomMouseEventArgs(button, clickCount, mouseHookStruct.pt.x, mouseHookStruct.pt.y, mouseDelta, state);

            //Raise it if not null.
            if (OnMouseActivity != null)
                OnMouseActivity(this, e);


            //Call next hook
            return CallNextHookEx(hMouseHook, nCode, wParam, lParam); //Not sure why, but it throws me an error.
        }
Пример #2
0
        /// <summary>
        /// MouseHook event method, detects the mouse clicks.
        /// </summary>
        private void MouseHookTarget(object sender, CustomMouseEventArgs keyEventArgs)
        {
            _recordClicked = keyEventArgs.Button == MouseButton.Left && keyEventArgs.State == MouseButtonState.Pressed;
            _posCursor = new System.Windows.Point(keyEventArgs.PosX, keyEventArgs.PosY);

            if (!IsMouseCaptured || Mouse.Captured == null)
                return;

            #region Get Handle and Window Rect

            var handle = Native.WindowFromPoint(keyEventArgs.PosX, keyEventArgs.PosY);
            var scale = this.Scale();

            if (_lastHandle != handle)
            {
                if (_lastHandle != IntPtr.Zero)
                    Native.DrawFrame(_lastHandle, scale);

                _lastHandle = handle;
                Native.DrawFrame(handle, scale);
            }

            Native.RECT rect;
            if (!Native.GetWindowRect(handle, out rect)) return;

            #endregion

            if (keyEventArgs.State == MouseButtonState.Pressed)
                return;

            #region Mouse Up

            Cursor = Cursors.Arrow;

            try
            {
                #region Try to get the process

                Process target = null;// Process.GetProcesses().FirstOrDefault(p => p.Handle == handle);

                var processes = Process.GetProcesses();
                foreach (var proc in processes)
                {
                    try
                    {
                        if (proc.MainWindowHandle != IntPtr.Zero && proc.HandleCount > 0 && (proc.Handle == handle || proc.MainWindowHandle == handle))
                        {
                            target = proc;
                            break;
                        }
                    }
                    catch (Exception)
                    { }
                }

                #endregion

                if (target != null && target.ProcessName == "ScreenToGif") return;

                //Clear up the selected window frame.
                Native.DrawFrame(handle, scale);
                _lastHandle = IntPtr.Zero;

                #region Values

                var top = (rect.Top / scale) - Constants.TopOffset;
                var left = (rect.Left / scale) - Constants.LeftOffset;
                var height = ((rect.Bottom - rect.Top + 1) / scale) + Constants.TopOffset + Constants.BottomOffset;
                var width = ((rect.Right - rect.Left + 1) / scale) + Constants.LeftOffset + Constants.RightOffset;

                #endregion

                #region Validate

                //TODO: Validate screensize...
                if (top < 0)
                    top = 0 - 1;
                if (left < 0)
                    left = 0 - 1;
                if (SystemInformation.VirtualScreen.Height < height + top)
                    height = SystemInformation.VirtualScreen.Height - top;
                if (SystemInformation.VirtualScreen.Width < width + left)
                    width = SystemInformation.VirtualScreen.Width - left;

                #endregion

                Top = top;
                Left = left;
                Height = height;
                Width = width;
            }
            catch (Exception ex)
            {
                LogWriter.Log(ex, "Error • Snap To Window");
            }
            finally
            {
                ReleaseMouseCapture();
            }

            #endregion
        }
Пример #3
0
        /// <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 int MouseHookProc(int nCode, int wParam, IntPtr lParam)
        {
            //If not ok and no one listens to our events, call next hook.
            if (nCode < 0 || OnMouseActivity == null)
            {
                return(CallNextHookEx(hMouseHook, nCode, wParam, lParam));
            }

            //Marshall the data from callback.
            var mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

            //Detect button clicked
            var   button     = MouseButton.XButton1;
            short mouseDelta = 0;
            var   state      = MouseButtonState.Pressed;

            #region Switch Mouse Actions

            switch (wParam)
            {
            case WM_LBUTTONDOWN:
                //case WM_LBUTTONUP:
                //case WM_LBUTTONDBLCLK:
                button = MouseButton.Left;
                break;

            case WM_LBUTTONUP:
                state  = MouseButtonState.Released;
                button = MouseButton.Left;
                break;

            case WM_RBUTTONDOWN:
                //case WM_RBUTTONUP:
                //case WM_RBUTTONDBLCLK:
                button = MouseButton.Right;
                break;

            case 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.
                button = MouseButton.Middle;
                break;

            case WM_MBUTTONDOWN:
                //case WM_MBUTTONUP:
                //case WM_MBUTTONDBLCLK:
                button = MouseButton.Middle;
                break;
                //default: I can't return now, it will break the click detector.
                //return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
                //HU3HU3 - A little funny momment: I just frooze my cursor by returning 1 instead of calling the next hook. - Nicke
                //Congrats to myself. ;D
                //05:24 AM 01/02/2014 (day-month-year)
            }

            #endregion

            //Double clicks
            int clickCount = 0;
            if (button != MouseButton.XButton1 || mouseDelta != 0)
            {
                clickCount = (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) ? 2 : 1;
            }

            //Generate event
            var e = new CustomMouseEventArgs(button, clickCount, mouseHookStruct.pt.x, mouseHookStruct.pt.y, mouseDelta, state);

            //Raise it if not null.
            if (OnMouseActivity != null)
            {
                OnMouseActivity(this, e);
            }


            //Call next hook
            return(CallNextHookEx(hMouseHook, nCode, wParam, lParam)); //Not sure why, but it throws me an error.
        }
Пример #4
0
 /// <summary>
 /// MouseHook event method, detects the mouse clicks.
 /// </summary>
 private void MouseHookTarget(object sender, CustomMouseEventArgs keyEventArgs)
 {
     _recordClicked = keyEventArgs.Button == MouseButton.Left;
 }
Пример #5
0
        /// <summary>
        /// MouseHook event method, detects the mouse clicks.
        /// </summary>
        private void MouseHookTarget(object sender, CustomMouseEventArgs keyEventArgs)
        {
            _recordClicked = keyEventArgs.Button == MouseButton.Left && keyEventArgs.State == MouseButtonState.Pressed;
            _posCursor = new System.Windows.Point(keyEventArgs.PosX, keyEventArgs.PosY);

            if (!IsMouseCaptured || Mouse.Captured == null || keyEventArgs.State == MouseButtonState.Pressed)
                return;

            //TODO: Make it work with parent handles...
            #region Mouse Up

            this.Cursor = Cursors.Arrow;

            try
            {
                var handle = Native.WindowFromPoint(keyEventArgs.PosX, keyEventArgs.PosY);
                var scale = this.Scale();

                Process target = null;// Process.GetProcesses().FirstOrDefault(p => p.Handle == handle);

                var processes = Process.GetProcesses();
                foreach (var proc in processes)
                {
                    try
                    {
                        if (proc.MainWindowHandle!= IntPtr.Zero && proc.HandleCount > 0 && (proc.Handle == handle || proc.MainWindowHandle == handle))
                        {
                            target = proc;
                            break;
                        }
                    }
                    catch (Exception)
                    {}
                }

                if (target == null || target.ProcessName == "ScreenToGif") return;

                Native.RECT rect;
                Native.GetWindowRect(handle, out rect);

                #region Values

                //TODO: Check the position, different OS'.
                var top = (rect.Top / scale) - 32;
                var left = (rect.Left / scale) - 6;
                var height = ((rect.Bottom - rect.Top + 1) / scale) + 58;
                var width = ((rect.Right - rect.Left + 1) / scale) + 12;

                #endregion

                #region Validate

                if (top < 0)
                    top = 0;
                if (left < 0)
                    left = 0;

                #endregion

                Top = top;
                Left = left;
                Height = height;
                Width = width;
            }
            catch (Exception ex)
            {
                LogWriter.Log(ex, "Error • Snap To Window");
            }
            finally
            {
                ReleaseMouseCapture();
            }

            #endregion
        }