对FlowLayoutPanel添加鼠标滚轮事件函数
在mainform中添加事件this
his.flowLayoutPanel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.flowLayoutPanel1_MouseWheel);
添加滚轮事件函数:spa
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的当前情况作判断。code
int lastRightPanelVerticalScrollValue = -1;//为鼠标滚动事件提供一个静态变量,用来存储上次滚动后的VerticalScroll.Value
添加FlowLayoutPanel的鼠标进入事件和鼠标点击事件orm
private void flowLayoutPanel1_MouseEnter(object sender, EventArgs e) { flowLayoutPanel1.Focus(); } private void flowLayoutPanel1_MouseClick(object sender, MouseEventArgs e) { flowLayoutPanel1.Focus(); }