C# 如何使用鼠标滚轮滚动面板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1600664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:14:53  来源:igfitidea点击:

How can I scroll my panel using my mousewheel?

c#scrollpanelmousewheel

提问by Sergio Tapia

I have a panel on my form with AutoScroll set to true so a scrollbar appears automatically.

我的表单上有一个面板,AutoScroll 设置为 true,因此滚动条会自动出现。

How can I make it so a user can use his mouse wheel to scroll the panel? Thanks SO.

我怎样才能让用户可以使用他的鼠标滚轮滚动面板?谢谢。

采纳答案by Jon B

The panel or a control in the panel must have focus. Note that if the control with focus has scroll bars, it will scroll instead of the panel.

面板或面板中的控件必须具有焦点。请注意,如果带焦点的控件有滚动条,它将滚动而不是面板。

回答by MusiGenesis

Moving the scroll wheel should trigger the control's MouseMove event. The MouseEventArgs argument has a property named Delta, which gives the (signed) number of notches that the mouse wheel has moved. You can use this property to scroll the panel.

移动滚轮应触发控件的 MouseMove 事件。MouseEventArgs 参数有一个名为 Delta 的属性,它给出了鼠标滚轮移动的(带符号的)凹槽数。您可以使用此属性滚动面板。

回答by Pavan Navali

In the designer file, you can add the following line of code. the MouseWheel event is not doumented in Events list in the Properties window.

在设计器文件中,您可以添加以下代码行。MouseWheel 事件不会出现在“属性”窗口的“事件”列表中。

this.Panel1.MouseWheel+= System.Windows.Forms.MouseEventHandler(this.Panel1_MouseWheel);

Panel1_MouseWheel will be triggered when you roll the mouse weel

滚动鼠标滚轮时会触发 Panel1_MouseWheel

Add the code in the .cs file

在 .cs 文件中添加代码

回答by Nivas

Below code works for me.....

下面的代码对我有用.....

    Public Form
{
InitializeComponent();  
this.MouseWheel += new MouseEventHandler(Panel1_MouseWheel);
}

 private void Panel1_MouseWheel(object sender, MouseEventArgs e)
        {
         panel1.Focus();
         }

回答by beam022

What worked for me was adding panel1_MouseEnterEventHandler:

对我panel1_MouseEnter有用的是添加EventHandler:

private void panel1_MouseEnter(object sender, EventArgs e)
{
    panel1.Focus();
}

回答by Nguyen Minh Hien

Make sure that your panel has focus. And this is simple code to scroll your panel scrollbar. Hope this help. :) enter code here

确保您的面板有焦点。这是滚动面板滚动条的简单代码。希望这有帮助。:) 在此处输入代码

int deltaScroll = 10;

if (e.Delta > 0)
{

    if (pnlContain.VerticalScroll.Value - deltaScroll >= pnlContain.VerticalScroll.Minimum)
        pnlContain.VerticalScroll.Value -= deltaScroll;
    else
        pnlContain.VerticalScroll.Value = pnlContain.VerticalScroll.Minimum;
}
else
{
    if (pnlContain.VerticalScroll.Value + deltaScroll <= pnlContain.VerticalScroll.Maximum)
        pnlContain.VerticalScroll.Value += deltaScroll;
    else
        pnlContain.VerticalScroll.Value = pnlContain.VerticalScroll.Maximum;
}

回答by CularBytes

I am using a windows form with BorderStyle set to none, where I use a panel to have all my controls in, so it looks nice (color difference and such..) was having the same issue while I had other forms that worked fine.

我正在使用 BorderStyle 设置为 none 的 Windows 窗体,在那里我使用一个面板来放置我的所有控件,所以它看起来不错(颜色差异等......)在我有其他可以正常工作的窗体时遇到了同样的问题。

What did I forgot:

我忘记了什么:

   public myForm()
   {
        InitializeComponent();
        this.DoubleBuffered = true;
   }

DoubleBuffered is magical I noticed..

我注意到 DoubleBuffered 很神奇。

回答by GrayDwarf

The solution(seen above) provided by Beam022 worked for me while many of the other solutions did not. In my case, I was attempting to scroll a DataGridView control with the mousewheel event.

Beam022 提供的解决方案(见上文)对我有用,而许多其他解决方案却没有。就我而言,我试图使用鼠标滚轮事件滚动 DataGridView 控件。

The DataGridView_MouseWheel event handler was being called but the FirstDisplayedScrollingRowIndex value never changed. The value was always '0' even after explicitly setting it to 1. It's as if the property were read only.

正在调用 DataGridView_MouseWheel 事件处理程序,但 FirstDisplayedScrollingRowIndex 值从未改变。即使将其显式设置为 1,该值也始终为“0”。就好像该属性是只读的。

Still repro's in .Net Framework 4.6.

仍然在 .Net Framework 4.6 中重现。

回答by Xan-Kun Clark-Davis

In my case, the whole client area of the panel was occupied by UserControls (not a single pixel of the inner area visible, except the scrollbars).

在我的例子中,面板的整个客户区都被 UserControls 占据(除了滚动条,内部​​区域没有一个像素可见)。

In this case the panel doesn't get the mouse-events and will never focus (apperently, clicking on the scrollbar does not count as "being inside the panel").

在这种情况下,面板不会获得鼠标事件并且永远不会聚焦(显然,单击滚动条不算作“在面板内”)。

I had to add the following lines to the constructor of my UserControl derived class:

我必须将以下行添加到我的 UserControl 派生类的构造函数中:

MouseEnter += delegate {
   Parent?.Focus();
};

Now it works fine, as I have no scrollable content in the UserControls.

现在它工作正常,因为我在 UserControls 中没有可滚动的内容。