Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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# 如何使鼠标滚轮在滚动条上工作_C#_Events_Scrollbar_Mousewheel - Fatal编程技术网

C# 如何使鼠标滚轮在滚动条上工作

C# 如何使鼠标滚轮在滚动条上工作,c#,events,scrollbar,mousewheel,C#,Events,Scrollbar,Mousewheel,我正在用C#运行一个应用程序,我需要为滚动条添加鼠标滚轮功能 我在需要滚动的窗口中对一些控件设置了Focus()。但是,它仍然不起作用。但是如果一个最小化应用程序并再次最大化并滚动而不点击任何其他按钮,它就会工作 如果单击任何其他控件,则无法使用滚动条上的鼠标滚轮功能。另外,我在一些控件上放置了Refresh() 问题可能是什么?解决方案是什么?添加鼠标悬停事件处理程序并在滚动条上聚焦() this.panel1.MouseHover += new System.EventHandler(pan

我正在用C#运行一个应用程序,我需要为滚动条添加鼠标滚轮功能

我在需要滚动的窗口中对一些控件设置了
Focus()
。但是,它仍然不起作用。但是如果一个最小化应用程序并再次最大化并滚动而不点击任何其他按钮,它就会工作

如果单击任何其他控件,则无法使用滚动条上的鼠标滚轮功能。另外,我在一些控件上放置了
Refresh()


问题可能是什么?解决方案是什么?

添加鼠标悬停事件处理程序并在滚动条上聚焦()

this.panel1.MouseHover += new System.EventHandler(panel1_MouseHover);

private void panel1_MouseHover (object sender, EventArgs e)
{
    this.vScrollBar1.Focus();
}

在滚动条上添加鼠标悬停事件处理程序和焦点()

this.panel1.MouseHover += new System.EventHandler(panel1_MouseHover);

private void panel1_MouseHover (object sender, EventArgs e)
{
    this.vScrollBar1.Focus();
}

这将产生滚动问题,不是滚动位置被重置,而是父容器正在将自身滚动到用户控件的左上角

为了避免这种情况,您必须重写
ScrollToControl
方法。扩展
System.Windows.Forms.Panel
并覆盖那里的
ScrollToControl
方法

示例代码:

class CustomScrollBarPanel : System.Windows.Forms.Panel
{
    protected override Point ScrollToControl(Control activeControl)
    {
        return this.AutoScrollPosition;
    }
}

然后使用它。

它会产生滚动问题,不是滚动位置被重置,而是父容器正在将自身滚动到用户控件的左上角

为了避免这种情况,您必须重写
ScrollToControl
方法。扩展
System.Windows.Forms.Panel
并覆盖那里的
ScrollToControl
方法

示例代码:

class CustomScrollBarPanel : System.Windows.Forms.Panel
{
    protected override Point ScrollToControl(Control activeControl)
    {
        return this.AutoScrollPosition;
    }
}

然后使用它。

您可以指定:这是WPF还是WinForms?可能是页面中某个控件上的@Hans Passant I set Focus()重复,但仍然不起作用。您可以指定:这是WPF还是WinForms?可能是页面中某个控件上的@Hans Passant I set Focus()重复,但仍然不起作用。