当前位置导航:炫浪网>>网络学院>>编程开发>>Visual C#教程

C#彻底解决listview的闪屏问题

也许很多人也遇到这样的问题,连续不断向listview中加载数据时界面会不停的闪烁,当你用listview.BeginUpdate(); listview.EndUpdate(); 虽然可以减轻一些,但也不能彻底消除闪屏。不信?可以试试下面的代码: public partial class Form1 : Form……{


        private int counter = 0;
        public Form1()
        ...{
            InitializeComponent();
        }
        DoubleBufferListView lv = new DoubleBufferListView();

        ListView lv = new ListView();
        private void Form1_Load(object sender, EventArgs e)
        ...{
            timer1.Enabled = false;
            this.Controls.Add(lv);
            lv.View = View.Details;
            lv.GridLines = true;
            lv.BackColor = Color.Silver;
            lv.Columns.Add("aa");
            lv.Columns.Add("bb");
            lv.Columns.Add("cc");
            lv.Height = 300;
            lv.Width = 300;
            timer1.Enabled = true;
         }
        private void timer1_Tick(object sender, EventArgs e)
        ...{
            lv .Items.Add(counter.ToString());
            counter++;
        }
     }
    解决方法:

    使用双缓冲,重写listview

    class DoubleBufferListView : ListView
        ...{
            public DoubleBufferListView()
            ...{
                SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
                UpdateStyles();
            }
        }
        public partial class Form1 : Form
        ...{
            private int counter = 0;
            public Form1()
            ...{
                InitializeComponent();

                 }
            DoubleBufferListView lv = new DoubleBufferListView();
            private void Form1_Load(object sender, EventArgs e)
            ...{
                timer1.Enabled = false;
                this.Controls.Add(lv);
                lv.View = View.Details;
                lv.GridLines = true;
                lv.BackColor = Color.Silver;
                lv.Columns.Add("aa");
                lv.Columns.Add("bb");
                lv.Columns.Add("cc");
                lv.Height = 300;
                lv.Width = 300;
                timer1.Enabled = true;
            }
            private void timer1_Tick(object sender, EventArgs e)
            ...{
                lv .Items.Add(counter.ToString());
                counter++;
            }
    效果是不是很明显啊?呵呵。

相关内容
赞助商链接