views:

154

answers:

3

I'm using ListView in VirtualMode and Details View with small icons.

This ListView have like 100,000 items inside.

The problem is that drawing this listview is much slower in Windows 7 compared to XP.

You can fill the slow drawing while scrolling the ListView or while multi-selecting items.

Additionally, i noticed that drawing become slower with each column added.

RetrieveVirtualItem event handler for now doing nothing but return literal values, so this is not the bottleneck.

Any ideas?

UPDATE: Source code for reproduce:

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;
    }
}
+3  A: 

I do measure a slowdown on this code, XP draws the listview in about 47 msec, Win7 needs about 96 msec, roughly twice as slow. Not sure if that deserves the 'hell' moniker. I don't know what causes it but don't doubt that Aero has something to do with it. Turning it off isn't exactly a desirable option.

Luckily you made a mistake in your code, one that costs 48 msec in my measurements. Giving you back the exact perf you had on XP. You re-use the same ListViewItem, you're supposed to create a new one. Add this line of code to the RetrieveVirtualItem event handler:

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

And get rid of the field.

Hans Passant
Tried this one, dont see any difference.
DxCK
Hmm, this repro-ed both on XP and Win7. I can't see what you see, measure it by overriding WndProc() and using Stopwatch to measure the time for m.Msg == 15.
Hans Passant
+1  A: 

Try to change the SetStyle line to :

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

UserPaint -

"If true, the control paints itself rather than the operating system doing so" (MSDN)

.

rursw1
Have seen weird behavior when using ControlStyles.UserPaint for an owner drawn ListView. I suggest one uses the DoubleBuffered property on the ListView (Is protected so one have to inherit and then set it in the constructor).
Rolf Kristensen
A: 

Try to use WS_EX_COMPOSITED, it improves performance of virtual list quite a bit:

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

Edited: At least it reduces drawing artifacts.

Dmitry Karpezo