首页 > 代码库 > WinForm ListControl MouseWheel Envent

WinForm ListControl MouseWheel Envent

最近在使用Listbox开发程序的时候, 遇到MouseWheel event 异常。

设置如下:

1. DrawModel:ownerDrawVariable.

2. InternalHeight:False.

3. ScrollAswalysVisible:True.

4. Overwrite DrawItem.

5. Overwrite MeasureItem.

问题是:当items 很多(超过一屏)的时候,HScrollbar enable auto, 这是如果鼠标滚轮翻屏的时候,会有2次闪烁,第二次是:最后一条item从屏幕顶端一下来,像是滚屏一样。使用鼠标点击scrollbar往下拉,没有问题。

解决办法:

重写 ListBox onm ouseWheel Envent:

 1  protected override void onm ouseWheel(MouseEventArgs e) 2         {12             int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;13             int numberOfPixelsToMove = (int)(numberOfTextLinesToMove * this.Font.Size);14             GraphicsPath mousePath = new GraphicsPath();15             if (numberOfPixelsToMove != 0)16             {17                 System.Drawing.Drawing2D.Matrix translateMatrix = new System.Drawing.Drawing2D.Matrix();18                 translateMatrix.Translate(0, numberOfPixelsToMove);19                 mousePath.Transform(translateMatrix);20             }21             this.Invalidate();22         }