我正在尝试均匀调整 a 的所有列的大小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;
//}
}
}
}
谢谢!!