对于水平滚动,使用 Shift + 滚轮相当常见。
这两者都相当容易捕获。我可以使用 MouseWheel 事件以及由 KeyDown、KeyUp 事件设置的标志来跟踪何时按下 Shift 键。
但是,如何真正触发水平滚动呢?我知道 WM_MOUSEHWHEEL,它可以用来触发该事件吗?
更新:
为一个System.Windows.Form
有一个HorizontalScroll
类型的属性HScrollProperties
。您可以操纵Value
该对象上的属性来更改水平滚动条的位置。但是,到目前为止,我还没有发现该对象可用的任何其他控件。
如果您正在创建自己的控件,派生自UserControl
or ScrollControl
or Form
,您可以使用这个简单的解决方案:
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);
}
}
解释
如果一个控件有AutoScroll
并显示滚动条,当您滚动鼠标滚轮时,您将得到以下行为:
- 如果没有启用滚动条,则不会执行任何操作。
- 如果仅启用垂直滚动条,则滚动垂直滚动条。
- 如果仅启用水平滚动条,则会滚动水平滚动条。
- 如果同时启用垂直和水平滚动条,则会滚动垂直滚动条。
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).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)