Shift + mouse wheel horizontal scrolling

后端 未结 3 764
无人共我
无人共我 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:16

    If you are creating 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);
        }
    }
    

    Explanation

    If a control has AutoScroll and is displaying scrollbars, when you scroll the mouse wheel you will get the following behaviour:

    1. If no scrollbars are enabled, it does nothing.
    2. If only vertical scrollbar is enabled, it scrolls vertical scrollbar.
    3. If only horizontal scrollbar is enabled, it scrolls horizontal scrollbar.
    4. If both vertical and horizontal scrollbars are enabled, it scrolls vertical scrollbar.

    Noticing this behaviour, I figured out this hack to override OnMouseWheel of the control, then if the vertical scrollbar is enabled and Shift is held down, it disables the vertical scrollbar before calling base.OnMouseWheel. This will trick the control in scrolling the horizontal scrollbar (behaviour 3 as shown above).

提交回复
热议问题