Winform ListView 窗体闪烁问题解决

在 winform 编程时, ListView 添加数据时 控件闪烁 , 参考如下解决方法,得到改善。

首先,自定义一个类ListViewNF,继承自 System.Windows.Forms.ListView

代码如下:

 public class ListViewNF : System.Windows.Forms.ListView
    {
        public ListViewNF():base()
        {
            // 开启双缓冲
            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;

最后大功告成。

软件界面如下:

参考:https://www.cnblogs.com/zdkjob/archive/2012/01/17/2324618.html

猜你喜欢

转载自www.cnblogs.com/howtrace/p/10689171.html