【问题标题】:C# listview imagelist fast add many itemsC# listview imagelist 快速添加很多项
【发布时间】:2012-06-24 15:42:01
【问题描述】:
我的表单上有一个 C# 中的 ListView 和 ImageList,并读取了一个最多包含大约 1000 个文件的目录。
我使用 AddRange 方法使用 fileItems DummyItems 的计数预先填充 ListView 和 ImageList,以避免 ListView 闪烁和闪烁。
现在在第二步中,我只想在从文件系统读取真实项目时将正确的项目信息分配给虚拟项目。
项目文本到目前为止没有问题,但我无法替换虚拟图像。如果我尝试这样做,它总是会抛出一个无效的参数异常。使用 RemoveAtIndex 或 RemoveAtKey 删除图像然后重新添加将花费我很长时间来遍历 1000 个文件。使用 ImageList 中的“RemoveAtKey”,1000 个文件需要 8 分钟。 “RemoveAtKey”是我发现的瓶颈。
如果我之前尝试清除所有图像并再次使用 AddRange 重新填充,我的项目图像将变为空白或发生异常。
有人知道我如何使用不同于我使用的其他方法从 1000 个具有文件名的文件中快速获取 1000 个不同的缩略图到列表视图控件中吗?
【问题讨论】:
标签:
c#
listview
imagelist
addrange
【解决方案1】:
首先,您可能希望使用以下代码创建一个名为“ListViewNF”的新用户控件:
class ListViewNF : System.Windows.Forms.ListView
{
public ListViewNF()
{
//Activate double buffering
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);
}
}
}
这解决了高速添加项目到列表视图时闪烁的问题。
我仍在为您的其他问题做一些研究和测试。