[英]Slow scroll on Winforms DataGridView
我正在使用DataGridView
,滾動行為非常糟糕。 當通過移動側面的滾動條進行滾動時,它的效果非常好,但是非常慢,並且使用鼠標快速滾動時,UI會凍結幾秒鍾。
我發現的現有問題建議設置DoubleBuffered
屬性,但是在我的情況下它不會影響性能,因為當我手動移動滾動條時,繪制已經很快。 問題僅在於使用鼠標滾輪滾動。
解決方案是在控件有機會處理鼠標滾輪事件的WndProc
之前,只需更改FirstDisplayedScrollingRowIndex
屬性即可。
public class FastScrollingDataGridView : DataGridView
{
private int numberOfRowsPerScroll = 20;
short GET_WHEEL_DELTA_WPARAM(IntPtr wParam)
{
var int32 = (int)wParam.ToInt64();
var shifted = int32 >> 16;
return (short)shifted;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x20a) //WM_MOUSEWHEEL = 0x20a
{
var zDelta = GET_WHEEL_DELTA_WPARAM(m.WParam) > 0 ? numberOfRowsPerScroll : -numberOfRowsPerScroll;
var newValue = FirstDisplayedScrollingRowIndex - zDelta;
if (newValue < 0) newValue = 0;
this.FirstDisplayedScrollingRowIndex = newValue;
return;
}
base.WndProc(ref m);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.