C#中listview控件拖动与刷新时出现闪烁问题

双缓冲方式解决listview闪烁问题,下面整理的代码测试正常,链接 https://zhidao.baidu.com/question/1445967546716046900.html

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.Interval = 100;
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        lv.Items.Add(counter.ToString());
        counter++;
    }
}

猜你喜欢

转载自blog.csdn.net/dd_zhouqian/article/details/89333742