Shift + mouse wheel horizontal scrolling

后端 未结 3 759
无人共我
无人共我 2020-12-31 16:40

The use of shift + scroll wheel is fairly common for horizontal scrolling.

Both of those are fairly easy to capture. I can use the MouseWheel event with a flag set

3条回答
  •  春和景丽
    2020-12-31 17:08

    In your designer file, you'll need to manually add a MouseWheel event delegate.

    this.richTextBox.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.RichTextBox_MouseWheel);
    

    Then, in your code behind, you can add the following.

        private const int WM_SCROLL = 276; // Horizontal scroll 
        private const int SB_LINELEFT = 0; // Scrolls one cell left 
        private const int SB_LINERIGHT = 1; // Scrolls one line right
    
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); 
    
        private void RichTextBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if (ModifierKeys == Keys.Shift)
            {
                var direction = e.Delta > 0 ? SB_LINELEFT : SB_LINERIGHT;
    
                SendMessage(this.richTextBox.Handle, WM_SCROLL, (IntPtr)direction, IntPtr.Zero);
            }
        }
    

    For more information on the const values, see the following SO: How do I programmatically scroll a winforms datagridview control?

    UPDATE

    Use Alvin's solution if possible. It's way better.

提交回复
热议问题