不行。。看看我的代码。 有没有哪里不对的地方!procedure AutoListVWidth(ListV : TlistView); var i,j,tmpWidth,tmpWidth1,titleWidth :integer; begin For j:=0 to ListV.Columns.count-1 do begin tmpWidth1:=0; TitleWidth:=Length(ListV.Columns.Items[j].Caption)*6; For i:=0 to ListV.Items.Count-1 do begin IF j=0 then begin tmpWidth:=Length(ListV.Items.Item[i].Caption)*6; IF TmpWidth>tmpWidth1 then tmpWidth1:=tmpWidth; End Else begin tmpWidth:=Length(ListV.Items.Item[i].subitems.strings[j-1])*6; IF TmpWidth>tmpWidth1 then tmpWidth1:=tmpWidth; End; End; IF tmpWidth1>TitleWidth then ListV.Columns.items[j].Width:=TmpWidth1 Else ListV.Columns.items[j].Width:=TitleWidth; ListV.Repaint; End; end;
Use Width to specify the width of the list column, in pixels. If Width is assigned the value -1, the list column is automatically resized to accommodate the text in the column. //如果你想那个列的宽度自动适应大小,只要把它设置为-1,就可以了。
如:"1234567890" length后。宽度是10
再*6
这一列的宽度就是60
还是不行。为什么要*6
我试了一下。
LAbel.width:=0;
LABEL.caption:='0';//一个英文字。
showmessage(inttostr(label.width));
显示为6;
我看了你的方法,应该可以的。
加上 ListView1.Repaint试试就是重画一下。
有没有哪里不对的地方!procedure AutoListVWidth(ListV : TlistView);
var
i,j,tmpWidth,tmpWidth1,titleWidth :integer;
begin
For j:=0 to ListV.Columns.count-1 do begin
tmpWidth1:=0;
TitleWidth:=Length(ListV.Columns.Items[j].Caption)*6;
For i:=0 to ListV.Items.Count-1 do begin
IF j=0 then begin
tmpWidth:=Length(ListV.Items.Item[i].Caption)*6;
IF TmpWidth>tmpWidth1 then tmpWidth1:=tmpWidth;
End
Else begin
tmpWidth:=Length(ListV.Items.Item[i].subitems.strings[j-1])*6;
IF TmpWidth>tmpWidth1 then tmpWidth1:=tmpWidth;
End;
End;
IF tmpWidth1>TitleWidth then ListV.Columns.items[j].Width:=TmpWidth1
Else ListV.Columns.items[j].Width:=TitleWidth;
ListV.Repaint;
End;
end;
1。 canvas.textwidth : 返回指定字符串的宽度(像素)。
2。 getdevicecap: 返回指定输出设备的信息。
这两个函数能够比较准确的计算各种输出的尺寸。