以编程方式更新 TListView 列 headers?
Update TListView column headers programmatically?
创建一个新的 VCL 表单应用程序并将此 TListview
粘贴到其表单上:
object ListView1: TListView
Left = 0
Top = 80
Width = 514
Height = 150
Align = alBottom
Columns = <
item
Caption = 'Caption'
Width = 100
end
item
Caption = 'AColumn'
Width = 100
end
item
Caption = 'BColumn'
Width = 100
end>
Items.ItemData = {
055A0000000100000000000000FFFFFFFFFFFFFFFF02000000FFFFFFFF000000
00054900740065006D0031000A5300750062004900740065006D004F006E0065
00603449440A5300750062004900740065006D00540077006F0048334944FFFF
FFFF}
TabOrder = 0
ViewStyle = vsReport
ExplicitLeft = 192
ExplicitTop = 128
ExplicitWidth = 250
end
然后在表单上放两个按钮Button1
和Button2
,这样表单单元就变成了这样:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;
type
TForm3 = class(TForm)
ListView1: TListView;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
ListView1.Column[2].AutoSize := True;
end;
procedure TForm3.Button2Click(Sender: TObject);
begin
ListView1.Width := ListView1.Width + 1;
end;
end.
运行 应用程序。单击 Button1
,将第三列的 AutoSize
属性 设置为 True
:这应将第三列的宽度扩展到 ListView 边框。但是,仅当您使用这些解决方法时,此更改才会可见:
通过用鼠标拖动表格边缘来扩展表格的宽度,或者:
单击以编程方式执行相同操作的第二个按钮。
但是如何在不使用这些解决方法的情况下以编程方式更新列 headers?
发送 WM_WINDOWPOSCHANGED
到 ListView:
var
wpos: TWindowPos;
begin
ListView1.Column[2].AutoSize := True;
FillChar(wpos, Sizeof(wpos), 0);
ListView1.Perform(WM_WINDOWPOSCHANGED, 0, @wpos);
end;
创建一个新的 VCL 表单应用程序并将此 TListview
粘贴到其表单上:
object ListView1: TListView
Left = 0
Top = 80
Width = 514
Height = 150
Align = alBottom
Columns = <
item
Caption = 'Caption'
Width = 100
end
item
Caption = 'AColumn'
Width = 100
end
item
Caption = 'BColumn'
Width = 100
end>
Items.ItemData = {
055A0000000100000000000000FFFFFFFFFFFFFFFF02000000FFFFFFFF000000
00054900740065006D0031000A5300750062004900740065006D004F006E0065
00603449440A5300750062004900740065006D00540077006F0048334944FFFF
FFFF}
TabOrder = 0
ViewStyle = vsReport
ExplicitLeft = 192
ExplicitTop = 128
ExplicitWidth = 250
end
然后在表单上放两个按钮Button1
和Button2
,这样表单单元就变成了这样:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;
type
TForm3 = class(TForm)
ListView1: TListView;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
ListView1.Column[2].AutoSize := True;
end;
procedure TForm3.Button2Click(Sender: TObject);
begin
ListView1.Width := ListView1.Width + 1;
end;
end.
运行 应用程序。单击 Button1
,将第三列的 AutoSize
属性 设置为 True
:这应将第三列的宽度扩展到 ListView 边框。但是,仅当您使用这些解决方法时,此更改才会可见:
通过用鼠标拖动表格边缘来扩展表格的宽度,或者:
单击以编程方式执行相同操作的第二个按钮。
但是如何在不使用这些解决方法的情况下以编程方式更新列 headers?
发送 WM_WINDOWPOSCHANGED
到 ListView:
var
wpos: TWindowPos;
begin
ListView1.Column[2].AutoSize := True;
FillChar(wpos, Sizeof(wpos), 0);
ListView1.Perform(WM_WINDOWPOSCHANGED, 0, @wpos);
end;