我们都熟悉鼠标点击并按住鼠标按钮,然后将鼠标移动到网格的边缘,列/行滚动并且选择区域增加的功能。
我有一个基于DataGridView的控件,由于性能问题,我必须关闭MultiSelect并自己处理选择过程,现在点击+按住滚动功能也被禁用了。
有什么建议可以重新编写此功能吗?
我考虑使用简单的MouseLeave事件,但我不确定如何确定它离开的位置,并实现动态滚动速度。
我们都熟悉鼠标点击并按住鼠标按钮,然后将鼠标移动到网格的边缘,列/行滚动并且选择区域增加的功能。
我有一个基于DataGridView的控件,由于性能问题,我必须关闭MultiSelect并自己处理选择过程,现在点击+按住滚动功能也被禁用了。
有什么建议可以重新编写此功能吗?
我考虑使用简单的MouseLeave事件,但我不确定如何确定它离开的位置,并实现动态滚动速度。
只需将以下代码添加到您的Form1_Load中
DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel);
这个是用于鼠标滚轮事件的。
void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
int scrollLines = SystemInformation.MouseWheelScrollLines;
if (e.Delta > 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex
= Math.Max(0, currentIndex - scrollLines);
}
else if (e.Delta < 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex
= currentIndex + scrollLines;
}
}
完整答案 您需要设置焦点DataGridview
private void DataGridView1_MouseEnter(object sender, EventArgs e)
{
DataGridView1.Focus();
}
then Add Mouse wheel event in Load function
DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel);
Finally Create Mouse wheel function
void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
int scrollLines = SystemInformation.MouseWheelScrollLines;
if (e.Delta > 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines);
}
else if (e.Delta < 0)
{
if (this.DataGridView1.Rows.Count > (currentIndex + scrollLines))
this.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines;
}
}
对我来说它很好用。
void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
int scrollLines = SystemInformation.MouseWheelScrollLines;
if (e.Delta > 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines);
}
else if (e.Delta < 0)
{
if (this.DataGridView1.Rows.Count > (currentIndex + scrollLines))
this.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines;
}
}