【C#】添加鼠标管轮事件

对FlowLayoutPanel添加鼠标滚轮事件

在mainform中添加事件

his.flowLayoutPanel1.MouseWheel +=
new System.Windows.Forms.MouseEventHandler(this.flowLayoutPanel1_MouseWheel);

添加滚轮事件函数:

private void flowLayoutPanel1_MouseWheel(object sender, MouseEventArgs e)
{
if (!(flowLayoutPanel1.VerticalScroll.Visible == false || (flowLayoutPanel1.VerticalScroll.Value == && e.Delta > ) || (flowLayoutPanel1.VerticalScroll.Value == lastRightPanelVerticalScrollValue && e.Delta < )))
{
flowLayoutPanel1.VerticalScroll.Value += ;
lastRightPanelVerticalScrollValue = flowLayoutPanel1.VerticalScroll.Value;
flowLayoutPanel1.Refresh();
flowLayoutPanel1.Invalidate();
flowLayoutPanel1.Update();
}
}

除此之外还要说的一点是,触发鼠标的滚动事件后,处理事件的函数参数 MouseEventArgs e 中有个Delta属性,默认情况下向上滚动e.Delta=120,向下滚动e.Delta=-120。以上的程序还不是特别完美,因为当Panel控件较大而没有显示滚动条时,或滚动条已在最上方而滚轮又是向上滚动,或滚动条已在最下方而滚轮又是向下滚动时,同样会执行Panel.Refresh();Panel.Invalidate();Panel.Update();等窗体重绘代码,占用较多资源。因此可以在执行这些代码前先对Panel的当前状况做判断。

int lastRightPanelVerticalScrollValue = -;//为鼠标滚动事件提供一个静态变量,用来存储上次滚动后的VerticalScroll.Value

添加FlowLayoutPanel的鼠标进入事件和鼠标点击事件

private void flowLayoutPanel1_MouseEnter(object sender, EventArgs e)
{
flowLayoutPanel1.Focus();
}
private void flowLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
{
flowLayoutPanel1.Focus();
}
上一篇:scrapy——4 —反爬措施—logging—重要参数—POST请求发送实战


下一篇:Java:延迟功能的Robot在Lunix系统上会报错