From 04e50d521a61a0e885aa9e0361033583c334d0d3 Mon Sep 17 00:00:00 2001 From: YFdyh000 Date: Thu, 16 Apr 2020 11:07:02 +0800 Subject: [PATCH] AutoResizeColumns speed up --- v2rayN/v2rayN/Base/ListViewFlickerFree.cs | 25 ++++++++++------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/v2rayN/v2rayN/Base/ListViewFlickerFree.cs b/v2rayN/v2rayN/Base/ListViewFlickerFree.cs index 5004e84a..ee6a5e21 100644 --- a/v2rayN/v2rayN/Base/ListViewFlickerFree.cs +++ b/v2rayN/v2rayN/Base/ListViewFlickerFree.cs @@ -18,31 +18,28 @@ namespace v2rayN.Base { try { - int MaxWidth = 0; Graphics graphics = this.CreateGraphics(); - string str; - int width; - + // 原生 ColumnHeaderAutoResizeStyle.ColumnContent 将忽略列头宽度 this.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); for (int i = 0; i < this.Columns.Count; i++) { ColumnHeader c = this.Columns[i]; - str = c.Text; - MaxWidth = c.Width; + int cWidth = c.Width; + string MaxStr = ""; + Font font = this.Items[0].SubItems[0].Font; foreach (ListViewItem item in this.Items) { - Font font = item.SubItems[i].Font; - str = item.SubItems[i].Text; - width = (int)graphics.MeasureString(str, font).Width; - if (width > MaxWidth) - { - MaxWidth = width; - } + // 整行视作相同字形,不单独计算每个单元格 + font = item.SubItems[i].Font; + string str = item.SubItems[i].Text; + if (str.Length > MaxStr.Length) // 未考虑非等宽问题 + MaxStr = str; } - c.Width = MaxWidth; + int strWidth = (int)graphics.MeasureString(MaxStr, font).Width; + c.Width = System.Math.Max(cWidth, strWidth); } } catch { }