C#使用ListView更新數據出現閃爍解決辦法


C#使用ListView更新數據出現閃爍解決辦法

在使用vs自動控件ListView控件時候,更新里面的部分代碼時候出現閃爍的情況

如圖:

 

解決以后:

 

解決辦法使用雙緩沖:添加新類繼承ListView 對其重寫

 1 public class DoubleBufferListView : ListView
 2     {
 3         public DoubleBufferListView()
 4         {
 5             SetStyle(ControlStyles.DoubleBuffer |
 6               ControlStyles.OptimizedDoubleBuffer |
 7               ControlStyles.AllPaintingInWmPaint, true);
 8             UpdateStyles();
 9         }
10     }

新建一個DemoTest測試

 

1.添加一個DoubleBufferListView的實例

       DoubleBufferListView doubleBufferListView1= new DoubleBufferListView();
            // 
            // doubleBufferListView1
            // 
            this.doubleBufferListView1.Font = new System.Drawing.Font("微軟雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.doubleBufferListView1.FullRowSelect = true;
            this.doubleBufferListView1.HideSelection = false;
            this.doubleBufferListView1.Location = new System.Drawing.Point(50, 37);
            this.doubleBufferListView1.Name = "doubleBufferListView1";
            this.doubleBufferListView1.Size = new System.Drawing.Size(400, 191);
            this.doubleBufferListView1.TabIndex = 2;
            this.doubleBufferListView1.UseCompatibleStateImageBehavior = false;
            this.doubleBufferListView1.View = System.Windows.Forms.View.Details;

2.將其添加到form窗體里面

 this.Controls.Add(this.doubleBufferListView1);

3.給添加列

        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);

4.隨便添加點內容

         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);    

 

5.點擊按鈕開始運行

 private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(PlayGame);
            if (state == false)
            {
                state = true;
                button1.Text = "停止";
                th.IsBackground = true;
                th.Name = "新線程";
                th.Start();
            }
            else
            {
                state = false;
                button1.Text = "開始";

            }
        }
        private void PlayGame()
        {
            Random r = new Random();
            while (state)
            {
                string temp = r.Next(0, 10).ToString();
                label1.Text = temp;
                this.doubleBufferListView1.Items[0].SubItems[1].Text = temp;
            }
        }

6.運行對比圖:

左側是解決閃屏后,右側是自帶的ListView效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM