3

我在 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;
    }
}
4

3 回答 3

4

我确实测量了这段代码的减速,XP 在大约 47 毫秒内绘制列表视图,Win7 需要大约 96 毫秒,大约慢了一倍。不确定这是否配得上“地狱”的绰号。我不知道是什么原因造成的,但不要怀疑 Aero 与它有关。关闭它并不是一个理想的选择。

幸运的是,您在代码中犯了一个错误,在我的测量中花费了 48 毫秒。让您恢复在 XP 上的确切性能。您重复使用相同的 ListViewItem,您应该创建一个新的。将这行代码添加到 RetrieveVirtualItem 事件处理程序:

    private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
    {
        var _item = new ListViewItem(new string[6]);
        // etc...
    }

并摆脱该领域。

于 2010-10-03T19:17:52.393 回答
1

尝试将 SetStyle 行更改为:

base.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); 

用户画 -

“如果为真,则控件绘制自身而不是操作系统绘制”(MSDN

.

于 2010-10-05T13:47:45.423 回答
0

尝试使用 WS_EX_COMPOSITED,它可以大大提高虚拟列表的性能:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;
        return cp;
    }
}

编辑:至少它减少了绘图伪影。

于 2010-10-05T14:51:33.637 回答