Horizontal Scrolling Shift + Mouse

Using the shift + scroll wheel is pretty common for horizontal scrolling.

Both of them are pretty easy to grip. I can use the MouseWheel event with the flag set by KeyDown, KeyUp events to track when the shift key is pressed.

However, how do I actually start horizontal scrolling? I know WM_MOUSEHWHEEL, can this be used to trigger an event?

Update: For a System.Windows.Form there is a HorizontalScroll property of type HScrollProperties . You can manipulate the Value attribute on this object to reposition the horizontal scrollbar. However, so far I have not found any other controls on which this object is available.

+7
source share
3 answers

If you create your own control derived from UserControl or ScrollControl or Form , you can use this simple solution:

 protected override void OnMouseWheel(MouseEventArgs e) { if (this.VScroll && (Control.ModifierKeys & Keys.Shift) == Keys.Shift) { this.VScroll = false; base.OnMouseWheel(e); this.VScroll = true; } else { base.OnMouseWheel(e); } } 

Description

If the control has AutoScroll and displays scroll bars, you will get the following behavior when scrolling the mouse wheel:

  • If scrollbars are not included, it does nothing.
  • If only the vertical scroll bar is enabled, it scrolls the vertical scroll bar.
  • If only the horizontal scroll bar is enabled, it scrolls the horizontal scroll bar.
  • If the vertical and horizontal scroll bars are enabled, it scrolls the vertical scroll bar.

Noting this behavior, I realized that this hack will override the OnMouseWheel of the control, then if the vertical scrollbar is turned on and Shift is held down, it disables the vertical scrollbar before calling base.OnMouseWheel . This will deceive control when scrolling the horizontal scrollbar (behavior 3, as shown above).

+10
source

In your designer file, you need to manually add the MouseWheel event delegate.

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

Then in your code 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 const values, see the following SO: How to programmatically scroll through a datagridview winforms control?

UPDATE

Use Alvin solution if possible. This is better .

+4
source

The same code provided by xixonia in VB.NET

 Private Const WM_SCROLL As Integer = 276 Private Const SB_LINELEFT As Integer = 0 Private Const SB_LINERIGHT As Integer = 1 <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer End Function Private Sub RichTextBox_MouseWheel(ByVal sender As Object, ByVal e As MouseEventArgs) Handles RichTextBox1.MouseWheel If ModifierKeys = Keys.Shift Then Dim direction = If(e.Delta > 0, SB_LINELEFT, SB_LINERIGHT) SendMessage(Me.RichTextBox1.Handle, WM_SCROLL, CType(direction, IntPtr), IntPtr.Zero) End If End Sub 
+1
source

All Articles