C#中使用ListView動態添加數據不閃爍

手頭正在做一個通訊網關,選用了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,都不會有半點的閃爍了,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章