listview为vsReport时,能否自动适应列宽(50)

  • 主题发起人 主题发起人 rgn
  • 开始时间 开始时间
R

rgn

Unregistered / Unconfirmed
GUEST, unregistred user!
listview为vsReport时,能否自动适应列宽
 
可以,Autosize设为True
 
好象没这属性吧~
 
我是这样做的~unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls;type TForm1 = class(TForm) lv1: TListView; procedure FormCreate(Sender: TObject); procedure lv1CustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean); procedure lv1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); private { Private declarations } FLvColumnWidths: array of Integer; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);var lvItem: TListItem; s: string; I: integer;begin if lv1.Columns.Count > 0 then begin SetLength(FLvColumnWidths, lv1.Columns.Count); // 设置初始值 for I := 0 to High(FLvColumnWidths) do FLvColumnWidths := 10;// FillChar(FLvColumnWidths[0], SizeOf(Integer) * Length(FLvColumnWidths), 10); end; s := '咗左基'; for i := 0 to 5 do begin s := s + 'A'; lvItem := lv1.Items.Add; lvItem.Caption := s; lvItem.SubItems.Add(s); lvItem.SubItems.Add(s); lvItem.SubItems.Add(s); end;end;procedure TForm1.lv1CustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean);var TmpStr: string; iTextWidth: Integer;begin with TListView(Sender) do begin TmpStr := Item.SubItems.Strings[SubItem -1]; iTextWidth := Canvas.TextWidth(TmpStr) + 15 + 15; if SubItem <= High(FLvColumnWidths) then if iTextWidth > FLvColumnWidths[SubItem] then begin FLvColumnWidths[SubItem] := iTextWidth; Columns[SubItem].Width := FLvColumnWidths[SubItem]; end; end;end;procedure TForm1.lv1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);var TmpStr: string; iTextWidth: Integer;begin with TListView(Sender) do begin TmpStr := Item.Caption; iTextWidth := Canvas.TextWidth(TmpStr) + 15 + 15; if iTextWidth > FLvColumnWidths[0] then begin FLvColumnWidths[0] := iTextWidth; Columns[0].Width := FLvColumnWidths[0]; end; end;end;end.
 
网不好重发了~
 
要根据标题和里面的内容自动适应列宽
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
767
DelphiTeacher的专栏
D
D
回复
0
查看
809
DelphiTeacher的专栏
D
后退
顶部