Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体:ctrl按钮防止ListView使用鼠标滚轮滚动_C#_.net - Fatal编程技术网

C# Windows窗体:ctrl按钮防止ListView使用鼠标滚轮滚动

C# Windows窗体:ctrl按钮防止ListView使用鼠标滚轮滚动,c#,.net,C#,.net,我想在按住Ctrl键的同时用鼠标滚轮滚动ListView。但显然按下Ctrl键会改变滚动行为:它会停止滚动,可能会尝试应用一些缩放逻辑,我不知道。我不知道该如何克服它。 请提供任何帮助或建议?按住Ctrl键时使鼠标滚轮滚动工作的解决方案是监听WndProc事件,并专门检测mouseweel触发器,这是最简单的工作示例: 带有WndProc覆盖的列表框 class CtrlListBoxScroll : ListBox { private const int WM_HSCROLL = 0x

我想在按住Ctrl键的同时用鼠标滚轮滚动ListView。但显然按下Ctrl键会改变滚动行为:它会停止滚动,可能会尝试应用一些缩放逻辑,我不知道。我不知道该如何克服它。
请提供任何帮助或建议?

按住Ctrl键时使鼠标滚轮滚动工作的解决方案是监听
WndProc
事件,并专门检测
mouseweel
触发器,这是最简单的工作示例:

带有
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;
            }
        }
    }
}
然后最后测试它

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);
}

什么样的项目?…可能是WinForms?@Idle\u介意吗?对不起,是的,Windows FormsBSCURE事实:传递给OnMouseWheel()的参数实际上是一个HandledMouseEventArgs。它允许您在方法重写中强制转换e参数,并将其Handled属性设置为true。删去了大量繁杂的代码。列表框和列表视图不是一回事,在列表视图中,可以有不同的视图在网格中显示内容(小图标、大图标等)。因此,在计算如何根据每行显示的内容数量进行设置时,代码会稍微复杂一些。
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);
}