手頭正在做一個通訊網關,選用了C#的WINFORM作界面
用了一個ListView來實時的顯示數據傳輸情況,於是問題就來了,當數據量比較大,而且處理速度很快時,這該死的界面閃得人眼花...
廢話不多說,直接上代碼:
首先,自定義一個類ListViewNF,繼承自 System.Windows.Forms.ListView
(NF=Never/No Flickering)
class ListViewNF : System.Windows.Forms.ListView
{
public ListViewNF()
{
// 開啟雙緩沖
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
// Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
然后,修改我們的Form代碼中定義ListView的位置,將原來的
System.Windows.Forms.ListView listView1;
修改為
ListViewNF listView1;
ok,然后隨便怎么insert\add這個listView1,都不會有半點的閃爍了,