[英]Windows Forms: ctrl button prevents ListView from scrolling with mouse wheel
我想在按下 Ctrl 按钮的同时用鼠标滚轮滚动 ListView。 但显然按下 Ctrl 会改变滚动行为:它停止滚动,可能会尝试应用一些缩放逻辑,我不知道。 我不知道如何覆盖它。 请任何帮助或建议?
在按住 Ctrl 键时让鼠标滚轮滚动的解决方案是侦听WndProc
事件并专门检测MOUSEWHEEL
触发器,最小的简单工作示例:
带有WndProc
覆盖的列表框
class CtrlListBoxScroll : ListBox
{
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
private const int WM_MOUSEWHEEL = 0x20A;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_MOUSEWHEEL)
{
var scrollDirection = NativeMethods.GET_WHEEL_DELTA_WPARAM(m.WParam);
// scrolling down
if (this.TopIndex < this.Items.Count && scrollDirection < 0)
{
this.TopIndex += 1;
}
// scrolling up
if (this.TopIndex > 0 && scrollDirection > 0)
{
this.TopIndex -= 1;
}
}
}
}
读取 wParam 并检测滚动方向的 NativeMethods
internal static class NativeMethods
{
internal static ushort HIWORD(IntPtr dwValue)
{
return (ushort)((((long)dwValue) >> 0x10) & 0xffff);
}
internal static ushort HIWORD(uint dwValue)
{
return (ushort)(dwValue >> 0x10);
}
internal static int GET_WHEEL_DELTA_WPARAM(IntPtr wParam)
{
return (short)HIWORD(wParam);
}
internal static int GET_WHEEL_DELTA_WPARAM(uint wParam)
{
return (short)HIWORD(wParam);
}
}
然后终于测试了
private void Form1_Load(object sender, EventArgs e)
{
var ctrlListBoxScroll = new CtrlListBoxScroll();
ctrlListBoxScroll.Items.AddRange
(
new object[]
{
"hello", "scroll", "bar", "pressing", "ctrl", "to scroll",
"this", "list", "box", "check", "ctrl", "key", "is", "held"
}
);
this.Controls.Add(ctrlListBoxScroll);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.