C# - 使用“滞后”调整ListView的大小

时间:2012-08-11 23:47:45

标签: c# listview resize lag blink

我正在尝试均匀调整ListView的所有列的大小,并且它可以正常工作,但我制作的代码是让屏幕闪烁并闪烁太多......我想知道我可以修复什么使代码顺利运行的代码? 这是代码:

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace Test_ListViews
{
    public partial class Form1 : Form
    {
        Double[] pesos;
        private bool resizing = false;
        private bool limpando = false; 

        public Form1()
        {
            InitializeComponent();

            int i = 0;

            float dx = 96;

            Graphics g = this.CreateGraphics();
            try
            {
                dx = g.DpiX;
            }
            finally
            {
                g.Dispose();
            }

            pesos = new Double[listView1.Columns.Count]; // usado para o resize das colunas da ListView ser proporcional.
            for (i = 0; i < listView1.Columns.Count; i++)
                pesos[i] = ((Double)listView1.Columns[i].Width * dx) / (listView1.Width * 96);
            _listView1_Resize();

            listView1.FullRowSelect = true;

            this.listView1.Resize += new System.EventHandler(this.listView1_Resize);
            this.listView1.ColumnWidthChanged += new System.Windows.Forms.ColumnWidthChangedEventHandler(this.listView1_ColumnWidthChanged);   
        }

        private void bntFill_Click(object sender, EventArgs e)
        {
            int i = 0;

            for (i = 0; i < 5; i++)
            {
                ListViewItem item = new ListViewItem("Test 1");
                item.SubItems.Add("Test 2");
                item.SubItems.Add("Test 3");
                item.SubItems.Add("Test 4");
                item.SubItems.Add("Test 5");
                item.SubItems.Add("Test 6");
                item.SubItems.Add("Test 7");
                item.SubItems.Add("Test 8");
                item.SubItems.Add("Test 9");
                listView1.Items.Add(item);
            }            

            SetWindowTheme(listView1.Handle, "Explorer", null);
        }

        [DllImport("uxtheme.dll")]
        public static extern int SetWindowTheme([In] IntPtr hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string pszSubAppName, [In, MarshalAs(UnmanagedType.LPWStr)] string pszSubIdList);

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();

            foreach(ListViewItem item in listView1.SelectedItems )
            {
                list.Add(item);
            }

            foreach (ListViewItem item in list)
            {
                listView1.Items.Remove(item);
            }
        }

        private void listView1_Resize(object sender, System.EventArgs e)
        {
            _listView1_Resize();
        }
        private void _listView1_Resize()
        {
            if (resizing == false && pesos != null)
            {
                resizing = true;

                Int32 largura = listView1.Width;
                int i = 0;


                for (i = 0; i < listView1.Columns.Count; i++)
                {
                    listView1.Columns[i].Width = Convert.ToInt32(pesos[i] * largura);
                }

                if (listView1.Controls.Count > 0)
                {
                    Int32 x = listView1.Items[0].SubItems[listView1.Items[0].SubItems.Count - 1].Bounds.Location.X + 3;//pegando a referencia da ultima coluna.

                    for (i = 0; i < listView1.Controls.Count; i++)
                    {
                        listView1.Controls[i].Location = new System.Drawing.Point(x, listView1.Controls[i].Location.Y);
                        listView1.Controls[i].Width = listView1.Columns[listView1.Columns.Count - 1].Width - 9;
                    }
                }

                if (listView1.Items.Count > 8)
                {
                    listView1.Columns[listView1.Columns.Count - 1].Width -= 10;
                }

                listView1.Scrollable = false;
                listView1.Scrollable = true;

                resizing = false;
            }

            SetWindowTheme(listView1.Handle, "Explorer", null);

        }

        private void listView1_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            _listView1_Resize();
            //int i = 0;
            //for (i = 0; i < listView1.Columns.Count; i++)
            //{
            //    if (listView1.Columns[i].Width < 20)
            //        listView1.Columns[i].Width = 20;
            //} 
        }

    }     
}

谢谢!

1 个答案:

答案 0 :(得分:1)

在调整大小操作开始时调用listView1.BeginUpdate(),然后在完成后调用listView1.EndUpdate()

这样,每当对它或其中任何一个孩子进行更改时,windows都不会重绘控件,并且当你调用EndUpdate()

时,它们会重绘一次。