C#中使用ListView控件的时候,频繁更新里面的数据时,会出现闪烁的情况。
解决办法:使用双缓冲,首先对listview类进行重写,然后通过继承重写的类,进行控件的初始化定义之后,再进行频繁数据更新是不会出现闪烁现象的。
//对ListView进行继承重写: public class DoubleBufferListView : ListView { public DoubleBufferListView() { SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles(); } }
下面为使用的一个实例:
//视图函数中定义一个Listview控件: DoubleBufferListView doubleBufferListView1= new DoubleBufferListView();this.doubleBufferListView1.Location = new System.Drawing.Point(50, 37); this.doubleBufferListView1.Name = "doubleBufferListView1"; this.doubleBufferListView1.Size = new System.Drawing.Size(400, 191);this.doubleBufferListView1.View = System.Windows.Forms.View.Details;
//添加listview到主视图 this.Controls.Add(this.doubleBufferListView1); //对控件进行列表头的添加 doubleBufferListView1.Clear(); doubleBufferListView1.Columns.Add("Action", 80, System.Windows.Forms.HorizontalAlignment.Left); doubleBufferListView1.Columns.Add("value", 80, System.Windows.Forms.HorizontalAlignment.Right); doubleBufferListView1.Columns.Add("Action", 80, System.Windows.Forms.HorizontalAlignment.Left); doubleBufferListView1.Columns.Add("value", 80, System.Windows.Forms.HorizontalAlignment.Left); //添加行数据 string[] listViewData = new string[4]; listViewData[0] = "Action"; listViewData[1] = "1"; listViewData[2] = "Action"; listViewData[3] = "1"; ListViewItem lvItem = new ListViewItem(listViewData, 0); doubleBufferView1.Items.Add(lvItem);