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

来源:互联网 发布:mac 显示所有应用 编辑:程序博客网 时间:2024/06/07 03:13
对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 == 0 && e.Delta > 0) || (flowLayoutPanel1.VerticalScroll.Value == lastRightPanelVerticalScrollValue && e.Delta < 0)))     {          flowLayoutPanel1.VerticalScroll.Value += 10;          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 = -1;//为鼠标滚动事件提供一个静态变量,用来存储上次滚动后的VerticalScroll.Value

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

private void flowLayoutPanel1_MouseEnter(object sender, EventArgs e){      flowLayoutPanel1.Focus();}private void flowLayoutPanel1_MouseClick(object sender, MouseEventArgs e){      flowLayoutPanel1.Focus();}

 

0 0
原创粉丝点击