创建一个新的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边框。但是,仅当您使用以下变通办法时,此更改才可见:
通过用鼠标拖动表格边缘来扩展表格的宽度,或者:
单击第二个按钮,以编程方式执行相同的操作。
但是如何在不使用这些替代方法的情况下以编程方式更新列标题?
答案 0 :(得分:3)
发送WM_WINDOWPOSCHANGED
到ListView:
var
wpos: TWindowPos;
begin
ListView1.Column[2].AutoSize := True;
FillChar(wpos, Sizeof(wpos), 0);
ListView1.Perform(WM_WINDOWPOSCHANGED, 0, @wpos);
end;