我在 VirtualMode 和带有小图标的详细信息视图中使用 ListView。
这个 ListView 里面有大约 100,000 个项目。
问题是与 XP 相比,在 Windows 7 中绘制此列表视图要慢得多。
您可以在滚动 ListView 或多选项目时填充慢速绘图。
此外,我注意到每添加一列,绘图就会变慢。
RetrieveVirtualItem 事件处理程序现在什么都不做,只返回文字值,所以这不是瓶颈。
有任何想法吗?
更新:用于重现的源代码:
FlickerFreeListView.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Diagnostics;
namespace ListViewTest
{
public class FlickerFreeListView : ListView
{
public FlickerFreeListView()
{
base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
}
}
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace ListViewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_item = new ListViewItem(new string[6]);
}
private ListViewItem _item;
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
string itemIndexStr = e.ItemIndex.ToString();
_item.Text = itemIndexStr;
_item.SubItems[1].Text = "blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla";
_item.SubItems[2].Text = itemIndexStr;
_item.SubItems[3].Text = itemIndexStr;
_item.SubItems[4].Text = itemIndexStr;
_item.SubItems[5].Text = itemIndexStr;
e.Item = _item;
}
}
}
Form1.Designer.cs:
namespace ListViewTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new ListViewTest.FlickerFreeListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// listView1
//
this.listView1.AutoArrange = false;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6});
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.FullRowSelect = true;
this.listView1.HideSelection = false;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(1032, 388);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.VirtualListSize = 100000;
this.listView1.VirtualMode = true;
this.listView1.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.listView1_RetrieveVirtualItem);
//
// columnHeader1
//
this.columnHeader1.Width = 92;
//
// columnHeader2
//
this.columnHeader2.Width = 405;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1032, 388);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
#endregion
private FlickerFreeListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;
}
}