Example #1
0
        private void HookOnMouseActivity(object sener, MouseExEventArgs e)
        {
            Point location = e.Location;

            if (e.Button == MouseButtons.Left)
            {
                Rectangle captionRect = new Rectangle(this.Location, new Size(this.Width, SystemInformation.CaptionHeight));
                if (captionRect.Contains(location))
                {
                    NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE,
                        (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) & (~KeyboardConstaint.WS_DISABLED));
                    NativeMethods.SendMessage(this.Handle, KeyboardConstaint.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero);
                }
                else {
                    NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE,
                        (int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) | KeyboardConstaint.WS_DISABLED);
                }
            }
        }
Example #2
0
        private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // if ok and someone listens to our events
            EventHandler<MouseExEventArgs> handler = this.Events[EventMouseActivity] as EventHandler<MouseExEventArgs>;
            if ((nCode >= 0) && (handler != null))
            {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button = MouseButtons.None;
                short mouseDelta = 0;
                switch ((int)wParam)
                {
                    case WM_LBUTTONDOWN:
                        // case WM_LBUTTONUP: 
                        // case WM_LBUTTONDBLCLK: 
                        button = MouseButtons.Left;
                        break;
                    case WM_RBUTTONDOWN:
                        // case WM_RBUTTONUP: 
                        // case WM_RBUTTONDBLCLK: 
                        button = MouseButtons.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 = unchecked((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;
                }

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                    if ((int)wParam == WM_LBUTTONDBLCLK || (int)wParam == WM_RBUTTONDBLCLK)
                        clickCount = 2;
                    else
                        clickCount = 1;

                //generate event 
                MouseExEventArgs e = new MouseExEventArgs(
                   button,
                   clickCount,
                   mouseHookStruct.pt.x,
                   mouseHookStruct.pt.y,
                   mouseDelta,
                   mouseHookStruct.flags
                );

                //raise it
                handler(this, e);
            }
            //call next hook
            return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
        }