使TStringGrid自适应宽度
下面请跟着图老师小编一起来了解下使TStringGrid自适应宽度,精心挑选的内容希望大家喜欢,不要忘记点个赞哦!
//传入TStringGrid对像即可, 2004/10/28,CoolSlob
var
I, J: Integer;
MaxLenRow: Integer;
CurRowLen: Integer;
begin
inherited;
for J := 0 to AGrids.ColCount - 1 do
begin
MaxLenRow := 0;
for I := 1 to AGrids.RowCount - 1 do
begin
CurRowLen := AGrids.Canvas.TextWidth(Trim(AGrids.Cells[J, I]));
//自适应宽度时,当值的最大宽度小于标题宽时,以标题宽为准。
if AGrids.Canvas.TextWidth(Trim(AGrids.Cells[J, 0])) CurRowLen then
CurRowLen := AGrids.Canvas.TextWidth(Trim(AGrids.Cells[J, 0]));
if CurRowLen MaxLenRow then
begin
MaxLenRow := CurRowLen + 5;
AGrids.ColWidths[J] := MaxLenRow;
end;
end;
end;
end;