var i,j:integer; iMax:array of integer; begin SetLength(iMax,lvwm.Columns.Count); for i:=0 to lvwm.Columns.Count-1 do begin iMax[i]:=0; end; for i:=0 to lvwm.Items.Count-1 do begin if iMax[0]<lvwm.Canvas.TextWidth(lvwm.Items.Item[i].Caption) then iMax[0]:=lvwm.Canvas.TextWidth(lvwm.Items.Item[i].Caption); for j:=1 to lvwm.Columns.Count-1 do begin if iMax[j]<lvwm.Canvas.TextWidth(lvwm.Items.Item[i].SubItems.Strings[j-1]) then iMax[j]:=lvwm.Canvas.TextWidth(lvwm.Items.Item[i].SubItems.Strings[j-1]); end; end; for i:=0 to lvwm.Columns.Count-1 do begin lvwm.Columns.Items[i].Width:=iMax[i]; end; end; 刚下去写了写,代码没有优化,在计算宽度上有点问题,子条目为空时也有问题,懒得再敲了(手累^_^),你自己改进吧。
i,j:integer;
iMax:array of integer;
begin
SetLength(iMax,lvwm.Columns.Count);
for i:=0 to lvwm.Columns.Count-1 do
begin
iMax[i]:=0;
end; for i:=0 to lvwm.Items.Count-1 do
begin
if iMax[0]<lvwm.Canvas.TextWidth(lvwm.Items.Item[i].Caption) then
iMax[0]:=lvwm.Canvas.TextWidth(lvwm.Items.Item[i].Caption);
for j:=1 to lvwm.Columns.Count-1 do
begin
if iMax[j]<lvwm.Canvas.TextWidth(lvwm.Items.Item[i].SubItems.Strings[j-1]) then
iMax[j]:=lvwm.Canvas.TextWidth(lvwm.Items.Item[i].SubItems.Strings[j-1]);
end;
end; for i:=0 to lvwm.Columns.Count-1 do
begin
lvwm.Columns.Items[i].Width:=iMax[i];
end;
end;
刚下去写了写,代码没有优化,在计算宽度上有点问题,子条目为空时也有问题,懒得再敲了(手累^_^),你自己改进吧。