代码之家  ›  专栏  ›  技术社区  ›  user1580348

以编程方式更新tlistview列标题?

  •  0
  • user1580348  · 技术社区  · 6 年前

    创建一个新的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.
    

    运行应用程序。点击 按钮1 它设置了 AutoSize 第三列的属性为 True 这应该扩展第三列的宽度直到ListView边界。但是,只有在使用以下解决方案时,此更改才可见:

    1. 用鼠标拖动窗体边缘以扩展窗体的宽度,或:

    2. 单击第二个按钮,它以编程方式执行相同的操作。

    但是,如果不使用这些解决方案,如何以编程方式更新列标题?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Uwe Raabe    6 年前

    发送一个 WM_WINDOWPOSCHANGED 到列表视图:

    var
      wpos: TWindowPos;
    begin
      ListView1.Column[2].AutoSize := True;
      FillChar(wpos, Sizeof(wpos), 0);
      ListView1.Perform(WM_WINDOWPOSCHANGED, 0, @wpos);
    end;