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;
}
}
}