Jump to content

Tilting the wheel


midora

Recommended Posts

Here is some code if you - the reader - like to support horizontal scrolling in your code:

        // ----------------------------------------------------------------------
        /// <summary>
        /// </summary>
        Timer _HWheelTimer;
        MouseButtons _HWheelButtons;
        int _HWheelDelta;
        int _HWheelX;
        int _HWheelY;

        // ----------------------------------------------------------------------
        /// <summary>
        /// Tilting of the mouse wheel may generate XBUTTON events instead of MOUSEHWHEEL (Logitech Mouse)
        /// The following code translates the XBUTTONS to MOUSEHWHEEL (<- respect the H ;-) 
        /// As long as the button is presssed (the wheel is tilted) the message will be repeated.
        /// By default the DefWindowProc translates WM_XBUTTONUP to WM_APPCOMMAND (APPCOMMAND_BROWSER_BACKWARD/FORWARD).
        /// </summary>
        private bool OnWM_XBUTTONDOWN(ref Message m)
        {
            Int32 lParam = (Int32)(Int64)m.LParam;
            Int32 wParam = (Int32)(Int64)m.WParam;
            int xbutton = wParam >> 16;
            int vkeyState = wParam & 0x0000FFFF;
            int x = (short)(lParam & 0x0000FFFF);
            int y = (short)(lParam >> 16);

            _HWheelButtons = 0;
            _HWheelDelta = 0;
            switch (xbutton)
            {
                case 1:
                    _HWheelButtons = MouseButtons.XButton1;
                    _HWheelDelta = 120;
                    break;
                case 2:
                    _HWheelButtons = MouseButtons.XButton2;
                    _HWheelDelta = -120;
                    break;
            }
            if (_HWheelButtons != 0)
            {
                _HWheelX = x;
                _HWheelY = y;
                HandleMouseWheel(new MouseEventArgs(_HWheelButtons, 0, _HWheelX, _HWheelY, _HWheelDelta), Orientation.Horizontal);
                _HWheelTimer.Stop();
                _HWheelTimer.Interval = 500;    // the interval will be decreasen on repeat
                _HWheelTimer.Start();
            }

            // MSDN: If an application processes this message, it should return TRUE.
            m.Result = (IntPtr)1;
            return true;
        }

        // ----------------------------------------------------------------------
        /// <summary>
        /// Repeat the last simulated horizontal wheel message
        /// </summary>
        void HWheelTimer_Tick(object sender, EventArgs e)
        {
            _HWheelTimer.Interval = 200;
            HandleMouseWheel(new MouseEventArgs(_HWheelButtons, 0, _HWheelX, _HWheelY, _HWheelDelta), Orientation.Horizontal);
        }

        // ----------------------------------------------------------------------
        /// <summary>
        /// Remarks: see OnWM_XBUTTONDOWN
        /// </summary>
        private bool OnWM_XBUTTONUP(ref Message m)
        {
            Int32 lParam = (Int32)(Int64)m.LParam;
            Int32 wParam = (Int32)(Int64)m.WParam;
            int xbutton = wParam >> 16;
            int vkeyState = wParam & 0x0000FFFF;
            int x = (short)(lParam & 0x0000FFFF);
            int y = (short)(lParam >> 16);

            if (_HWheelTimer != null)
            {
                _HWheelTimer.Stop();
            }

            // MSDN: If an application processes this message, it should return TRUE.
            m.Result = (IntPtr)1;
            return true;
        }

        // ----------------------------------------------------------------------
        /// <summary>
        /// </summary>
        private bool OnWM_MOUSEHWHEEL(ref Message m)
        {
            Int32 lParam = (Int32)(Int64)m.LParam;
            Int32 wParam = (Int32)(Int64)m.WParam;

            Int32 delta = (short)(wParam >> 16);
            Int32 vkeyState = wParam & 0xFFFF;
            int x = (short)(lParam & 0x0000FFFF);
            int y = (short)(lParam >> 16);

            HandleMouseWheel(new MouseEventArgs(Control.MouseButtons, 0, x, y, delta), Orientation.Horizontal);
            
            // MSDN: If an application processes this message, it should return zero
            m.Result = (IntPtr)0;
            return true;
        }

        // ----------------------------------------------------------------------
        /// <summary>
        /// </summary>
        protected override void WndProc(ref Message m)
        {
            const int WM_APPCOMMAND = 0x0319;
            // const int WM_MOUSEWHEEL = 0x020A;
            const int WM_XBUTTONDOWN = 0x020B;
            const int WM_XBUTTONUP = 0x020C;
            // const int WM_XBUTTONDBLCLK = 0x020D;
            const int WM_MOUSEHWHEEL = 0x020E;
            // const int WM_HSCROLL = 0x0114;
            // const int WM_VSCROLL = 0x0115;

            if (m.HWnd == this.Handle)
            {
                switch (m.Msg)
                {
                    case WM_APPCOMMAND:
                        if (OnWM_APPCOMMAND(ref m)) return;
                        break;

                    case WM_XBUTTONDOWN:
                        if (OnWM_XBUTTONDOWN(ref m)) return;
                        break;
                    case WM_XBUTTONUP:
                        if (OnWM_XBUTTONUP(ref m)) return;
                        break;
                    case WM_MOUSEHWHEEL:    // horizontal wheel
                        if (OnWM_MOUSEHWHEEL(ref m)) return;
                        break;
                }
            }

            base.WndProc(ref m);
        }
        

midoras signature.gif

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...