WinForm DataGridView 鼠标滚轮实现

转自:http://blog.csdn.net/fanglei1234567890/article/details/8791730?locationNum=10

 

 

Q:DataGridView 在鼠标移到滚动栏时,滚动才有效,怎样当我的光标在其中滚动就有效呢?

1:首先引用API函数WindowFromPoint(该函数获得包含指定点的窗口的句柄),判断当前鼠标指针是否在GridView中

 [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
 static extern IntPtr WindowFromPoint(Point pt);
 2:为GridView增加鼠标滚动事件
 private void orderGrid_MouseEnter(object sender, EventArgs e)
  {
       this.MouseWheel += orderGrid_MouseWheel;
  }
  public void orderGrid_MouseWheel(object sender, MouseEventArgs e)
   {     
         Point p = PointToScreen(e.Location);
         if ((WindowFromPoint(p)) == orderGrid.Handle)//鼠标指针在框内
         {
                if (e.Delta > 0)
                {
                    if (orderGrid.FirstDisplayedScrollingRowIndex - 5 < 0)
                    {
                        orderGrid.FirstDisplayedScrollingRowIndex = 0;
                    }
                    else
                    {
                        orderGrid.FirstDisplayedScrollingRowIndex = orderGrid.FirstDisplayedScrollingRowIndex - 5;
                    }
                }
                else
                {
                    orderGrid.FirstDisplayedScrollingRowIndex = orderGrid.FirstDisplayedScrollingRowIndex + 5;
                }
            }
     }

 

posted @ 2017-02-16 15:02  风中寻觅  阅读(1267)  评论(0)    收藏  举报