如何在虚拟模式下自动调整列表视图的列宽?

发布于 2025-01-04 18:07:17 字数 321 浏览 2 评论 0原文

当我使用 TListView (ViewStyle = vsReport) 时,我可以自动调整列的宽度,设置 LVSCW_AUTOSIZELVSCW_AUTOSIZE_USEHEADER 中的值每列的宽度属性,现在我开始在虚拟模式下使用Listview,但是列的宽度没有根据这些值进行修改。所以问题是:当 lisvtiew 处于虚拟模式时,如何调整列的宽度以适应内容或标题?

When I use a TListView (ViewStyle = vsReport) I can autofit the width of the columns setting the LVSCW_AUTOSIZE or LVSCW_AUTOSIZE_USEHEADER values in the Width property of each column, now I start to use the Listview in virtual mode, but the width of the columns is not modified according to these values. So the question is : How I can adjust the width of the columns to fit to the content or header, when the lisvtiew is in virtual mode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

︶ ̄淡然 2025-01-11 18:07:17

由于虚拟模式下的列表视图事先不知道项目标题(因为它只要求可见区域的数据),它也无法知道最宽的标题的宽度,所以这就是 < 的 autosize 标志的原因a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb761163%28v=vs.85%29.aspx" rel="noreferrer">LVM_SETCOLUMNWIDTH 的行为方式如下。

因此,唯一的方法是编写一个自定义函数来查询所有数据,测量所有未来字幕的文本宽度并将列宽设置为最宽的值。

以下示例显示如何执行此操作。它使用 ListView_GetStringWidth 宏用于文本宽度计算(这似乎是最自然的方法)。然而问题是文本填充的值。正如文档中所述:

ListView_GetStringWidth 宏返回精确的宽度(以像素为单位),
指定字符串的。如果使用返回的字符串宽度作为
调用 ListView_SetColumnWidth 宏时的列宽,
字符串将被截断。检索可以的列宽
包含字符串而不截断它,您必须向
返回字符串宽度。

但他们没有提到如何获取填充值(似乎 他们不会这样做)。有些人说(例如此处)使用 6 px 作为项目的填充,使用 12 px 作为子项目的填充就足够了,但事实并非如此(至少对于 Windows 7 上的这个示例)。

///////////////////////////////////////////////////////////////////////////////
/////   List View Column Autosize (Virtual Mode)   ////////////////////////////
///////////////////////////////////////////////////////////////////////////////

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, StdCtrls,
  Forms, Dialogs, StrUtils, ComCtrls, CommCtrl;

type
  TSampleRecord = record
    Column1: string;
    Column2: string;
    Column3: string;
  end;
  TSampleArray = array [0..49] of TSampleRecord;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    SampleArray: TSampleArray;
    procedure AutoResizeColumn(const AListView: TListView;
      const AColumn: Integer);
    procedure OnListViewData(Sender: TObject; Item: TListItem);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.AutoResizeColumn - auto-size column   //////////////////////////
///////////////////////////////////////////////////////////////////////////////

// AListView - list view object instance
// AColumn - index of the column to be auto-sized

procedure TForm1.AutoResizeColumn(const AListView: TListView;
  const AColumn: Integer);
var
  S: string;
  I: Integer;
  MaxWidth: Integer;
  ItemWidth: Integer;
begin
  // set the destination column width to the column's caption width
  // later on we'll check if we have a wider item
  MaxWidth := ListView_GetStringWidth(AListView.Handle,
    PChar(AListView.Columns.Items[AColumn].Caption));
  // iterate through all data items and check if their captions are
  // wider than the currently widest item if so then store that value
  for I := 0 to High(SampleArray) do
  begin
    case AColumn of
      0: S := SampleArray[I].Column1;
      1: S := SampleArray[I].Column2;
      2: S := SampleArray[I].Column3;
    end;
    ItemWidth := ListView_GetStringWidth(AListView.Handle, PChar(S));
    if MaxWidth < ItemWidth then
      MaxWidth := ItemWidth;
  end;
  // here is hard to say what value to use for padding to prevent the
  // string to be truncated; I've found the suggestions to use 6 px
  // for item caption padding and 12 px for subitem caption padding,
  // but a few quick tests confirmed me to use at least 7 px for items
  // and 14 px for subitems
  if AColumn = 0 then
    MaxWidth := MaxWidth + 7
  else
    MaxWidth := MaxWidth + 14;
  // and here we set the column width with caption padding included
  AListView.Columns.Items[AColumn].Width := MaxWidth;
end;

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.FormCreate - setup the list view and fill custom data   ////////
///////////////////////////////////////////////////////////////////////////////

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  ListView1.ViewStyle := vsReport;
  ListView1.Columns.Add.Caption := 'Column 1';
  ListView1.Columns.Add.Caption := 'Column 2';
  ListView1.Columns.Add.Caption := 'Column 3';
  ListView1.OwnerData := True;
  ListView1.OnData := OnListViewData;
  ListView1.Items.Count := High(SampleArray);

  for I := 0 to High(SampleArray) do
  begin
    SampleArray[I].Column1 := 'Cell [0, ' + IntToStr(I) + '] ' +
      DupeString('x', I);
    SampleArray[I].Column2 := 'Cell [1, ' + IntToStr(I) + '] ' +
      DupeString('x', High(SampleArray) - I);
    SampleArray[I].Column3 := '';
  end;
end;

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.FormCreate - custom handler for OnData event   /////////////////
///////////////////////////////////////////////////////////////////////////////

procedure TForm1.OnListViewData(Sender: TObject; Item: TListItem);
begin
  Item.Caption := SampleArray[Item.Index].Column1;
  Item.SubItems.Add(SampleArray[Item.Index].Column2);
  Item.SubItems.Add(SampleArray[Item.Index].Column3);
end;

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.Button1Click - auto-resize all 3 columns   /////////////////////
///////////////////////////////////////////////////////////////////////////////

procedure TForm1.Button1Click(Sender: TObject);
begin
  AutoResizeColumn(ListView1, 0);
  AutoResizeColumn(ListView1, 1);
  AutoResizeColumn(ListView1, 2);
end;

end.

Since the list view in virtual mode don't know the item captions in advance (because it asks only for data of the visible area) it also cannot know the width of the widest one, so that's the reason why the autosize flag of the LVM_SETCOLUMNWIDTH behaves this way.

Thus the only way is to write a custom function which will query all your data, measure the text widths of all future captions and set the column width to the value of the widest one.

The following example shows how to do it. It uses the ListView_GetStringWidth macro for the text width calculations (it seems to be the most natural way to do this). However the problem is the value of the text padding. As it's stated in the documentation:

The ListView_GetStringWidth macro returns the exact width, in pixels,
of the specified string. If you use the returned string width as the
column width in a call to the ListView_SetColumnWidth macro, the
string will be truncated. To retrieve the column width that can
contain the string without truncating it, you must add padding to the
returned string width.

But they didn't mention there how to get the padding value (and seems they won't to do so). Some people say (e.g. here) that it's enough to use 6 px for item's padding and 12 px for subitem's padding, but it isn't (at least for this example on Windows 7).

///////////////////////////////////////////////////////////////////////////////
/////   List View Column Autosize (Virtual Mode)   ////////////////////////////
///////////////////////////////////////////////////////////////////////////////

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, StdCtrls,
  Forms, Dialogs, StrUtils, ComCtrls, CommCtrl;

type
  TSampleRecord = record
    Column1: string;
    Column2: string;
    Column3: string;
  end;
  TSampleArray = array [0..49] of TSampleRecord;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    SampleArray: TSampleArray;
    procedure AutoResizeColumn(const AListView: TListView;
      const AColumn: Integer);
    procedure OnListViewData(Sender: TObject; Item: TListItem);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.AutoResizeColumn - auto-size column   //////////////////////////
///////////////////////////////////////////////////////////////////////////////

// AListView - list view object instance
// AColumn - index of the column to be auto-sized

procedure TForm1.AutoResizeColumn(const AListView: TListView;
  const AColumn: Integer);
var
  S: string;
  I: Integer;
  MaxWidth: Integer;
  ItemWidth: Integer;
begin
  // set the destination column width to the column's caption width
  // later on we'll check if we have a wider item
  MaxWidth := ListView_GetStringWidth(AListView.Handle,
    PChar(AListView.Columns.Items[AColumn].Caption));
  // iterate through all data items and check if their captions are
  // wider than the currently widest item if so then store that value
  for I := 0 to High(SampleArray) do
  begin
    case AColumn of
      0: S := SampleArray[I].Column1;
      1: S := SampleArray[I].Column2;
      2: S := SampleArray[I].Column3;
    end;
    ItemWidth := ListView_GetStringWidth(AListView.Handle, PChar(S));
    if MaxWidth < ItemWidth then
      MaxWidth := ItemWidth;
  end;
  // here is hard to say what value to use for padding to prevent the
  // string to be truncated; I've found the suggestions to use 6 px
  // for item caption padding and 12 px for subitem caption padding,
  // but a few quick tests confirmed me to use at least 7 px for items
  // and 14 px for subitems
  if AColumn = 0 then
    MaxWidth := MaxWidth + 7
  else
    MaxWidth := MaxWidth + 14;
  // and here we set the column width with caption padding included
  AListView.Columns.Items[AColumn].Width := MaxWidth;
end;

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.FormCreate - setup the list view and fill custom data   ////////
///////////////////////////////////////////////////////////////////////////////

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  ListView1.ViewStyle := vsReport;
  ListView1.Columns.Add.Caption := 'Column 1';
  ListView1.Columns.Add.Caption := 'Column 2';
  ListView1.Columns.Add.Caption := 'Column 3';
  ListView1.OwnerData := True;
  ListView1.OnData := OnListViewData;
  ListView1.Items.Count := High(SampleArray);

  for I := 0 to High(SampleArray) do
  begin
    SampleArray[I].Column1 := 'Cell [0, ' + IntToStr(I) + '] ' +
      DupeString('x', I);
    SampleArray[I].Column2 := 'Cell [1, ' + IntToStr(I) + '] ' +
      DupeString('x', High(SampleArray) - I);
    SampleArray[I].Column3 := '';
  end;
end;

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.FormCreate - custom handler for OnData event   /////////////////
///////////////////////////////////////////////////////////////////////////////

procedure TForm1.OnListViewData(Sender: TObject; Item: TListItem);
begin
  Item.Caption := SampleArray[Item.Index].Column1;
  Item.SubItems.Add(SampleArray[Item.Index].Column2);
  Item.SubItems.Add(SampleArray[Item.Index].Column3);
end;

///////////////////////////////////////////////////////////////////////////////
/////   TForm1.Button1Click - auto-resize all 3 columns   /////////////////////
///////////////////////////////////////////////////////////////////////////////

procedure TForm1.Button1Click(Sender: TObject);
begin
  AutoResizeColumn(ListView1, 0);
  AutoResizeColumn(ListView1, 1);
  AutoResizeColumn(ListView1, 2);
end;

end.
左岸枫 2025-01-11 18:07:17

考虑这个由 < 编写的辅助功能单元 a href="https://stackoverflow.com/users/91299/rruz">RRUZ。

辅助函数摘录:

procedure AutoResizeColumn(const Column:TListColumn;const Mode:Integer=LVSCW_AUTOSIZE_BESTFIT);
procedure AutoResizeColumns(const Columns : Array of TListColumn;const Mode:Integer=LVSCW_AUTOSIZE_BESTFIT);
procedure AutoResizeListView(const ListView : TListView;const Mode:Integer=LVSCW_AUTOSIZE_BESTFIT);

模式(参数)可以是:

  • LVSCW_AUTOSIZE_BESTFIT
  • LVSCW_AUTOSIZE
  • LVSCW_AUTOSIZE_USEHEADER

我希望它将作为您的要求的一个良好起点。

Consider this helper function unit written by RRUZ.

Excerpt of the helper functions:

procedure AutoResizeColumn(const Column:TListColumn;const Mode:Integer=LVSCW_AUTOSIZE_BESTFIT);
procedure AutoResizeColumns(const Columns : Array of TListColumn;const Mode:Integer=LVSCW_AUTOSIZE_BESTFIT);
procedure AutoResizeListView(const ListView : TListView;const Mode:Integer=LVSCW_AUTOSIZE_BESTFIT);

Mode (Parameter) could be:

  • LVSCW_AUTOSIZE_BESTFIT
  • LVSCW_AUTOSIZE
  • LVSCW_AUTOSIZE_USEHEADER

I hope it will serve as a good starting point for your requirement.

划一舟意中人 2025-01-11 18:07:17

这是避免列太窄的另一种可能的解决方案。但是,它需要一些有关需要显示的数据的知识,因此它不是通用解决方案。

使用最长/最宽的数据项构建 ListViewItem。切换到非虚拟模式并仅添加此最大 ListViewItem。根据最大项自动调整列宽,然后删除最大项,并切换回虚拟模式。例如:

// build a ListViewItem with longest data items
string[] items = new string[2];
items[0] = "999999"; // number
items[1] = "99:59:59.999"; // time hh:mm:ss.ttt
ListViewItem lviMax = new ListViewItem (items);
lv.VirtualMode = false; // switch to non-virtual mode
lv.Items.Clear (); // empty the row/line collection
lv.Visible = false; // so user doesnt see the fake values
lv.Items.Add (lviMax); // add line(s) with longest possible data items
lv.AutoResizeColumns (ColumnHeaderAutoResizeStyle.ColumnContent); // adjust column width
lv.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize); // adjust column width
lv.Items.Clear (); // empty row/line collection
lv.Visible = true;
lv.VirtualMode = true; // switch back to virtual mode

根据您的示例格式值,某些列现在可能有点太宽,但至少没有列会太窄..

Here is another possible solution to avoid too narrow columns. However, it requires some knowledge about the data you need to display, so it is not a general solution ..

Build a ListViewItem using the longest/widest data items. Switch to non-virtual mode and add just this maximum ListViewItem. Auto-adjust the column width based on the max item, then delete the max item, and switch back to virtual mode. E.g.:

// build a ListViewItem with longest data items
string[] items = new string[2];
items[0] = "999999"; // number
items[1] = "99:59:59.999"; // time hh:mm:ss.ttt
ListViewItem lviMax = new ListViewItem (items);
lv.VirtualMode = false; // switch to non-virtual mode
lv.Items.Clear (); // empty the row/line collection
lv.Visible = false; // so user doesnt see the fake values
lv.Items.Add (lviMax); // add line(s) with longest possible data items
lv.AutoResizeColumns (ColumnHeaderAutoResizeStyle.ColumnContent); // adjust column width
lv.AutoResizeColumns (ColumnHeaderAutoResizeStyle.HeaderSize); // adjust column width
lv.Items.Clear (); // empty row/line collection
lv.Visible = true;
lv.VirtualMode = true; // switch back to virtual mode

Depending on your sample format values, some columns now might be a bit too wide, but at least no column will be too narrow ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文