C#如何使FlowLayoutPanel支持中键滚轮滚动?
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/433
FlowLayoutPanel控件不直接支持MouseWheel事件.即滚动滚轮也不会响应.所以必须手动来支持响应滚轮.
查看了一下FlowLayoutPanel控件的源码,原来FlowLayoutPanel控件是继承于Panel控件的.
所以,Panel控件也是直接不支持MouseWheel事件来进行滚动滚轮的.
源码:
///
/// 支持滚轮的FlowLayoutPanel.
///
class ScrollFlowLayoutPanel : FlowLayoutPanel
{
public ScrollFlowLayoutPanel()
{
}
protected override void OnMouseClick(MouseEventArgs e)
{
this.Focus();
base.OnMouseClick(e);
}
protected override void OnMouseEnter(EventArgs e)
{
this.Focus();
base.OnMouseEnter(e);
}
protected override void OnMouseWheel(MouseEventArgs e)
{
if (e.Delta < 0)
{
if (this.VerticalScroll.Maximum > this.VerticalScroll.Value + 50)
this.VerticalScroll.Value += 50;
else
this.VerticalScroll.Value = this.VerticalScroll.Maximum;
}
else
{
if (this.VerticalScroll.Value > 50)
this.VerticalScroll.Value -= 50;
else
{
this.VerticalScroll.Value = 0;
}
}
}
}
使用方法:
在Winform的后台代码添加了这个类之后,你就可以从工具箱里面找到ScrollFlowLayoutPanel。
与FlowLayoutPanel不同的是,你可以从它的属性中找到AutoScroll,并可以设置为True。
补充:
但是实际测试,会有抖动。(原因暂时未知)
参考来源:
http://blog.sina.com.cn/s/blog_6b965dd70101po76.html
This article was last edited at 2020-08-21 15:58:57