C#中使用ListView动态添加数据不闪烁(网上方法会出问题)

it2022-05-08  6

最近需要使用做一个动态行显示,所以就用到了ListView控件,在网上也查到了关于动态添加不闪烁的方式都是如下:

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

(NF=Never/No Flickering)

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

 

ok,然后随便怎么insert\add这个listView1,都不会有半点的闪烁了。

注意!!!注意!!!注意!!!

这种方式是对的,但是我的项目是X64的,所以会一直报错,被坑了半天,发现这种创建的自定义控件如何创建对象放在系统控件的xxxxx.Designer.cs中就会报错,因为直接基于系统控件修改只能在x86中运行,不然会出现以下错误

这种有一种方法可以处理,就是直接把对象定义在功能代码类中,动态的创建对象就可以避免这种问题,当然也可以不使用这种方式消除动态闪烁问题,有的话大家可以交流一下。

 

转载于:https://www.cnblogs.com/xiaobaidashu/p/10035374.html

相关资源:垃圾分类数据集及代码

最新回复(0)