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; } } }
[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;
}
}
}
风中代表自由、寻觅代表不断前进~!

浙公网安备 33010602011771号