TStringGrid自適應寬度

使TStringGrid自適應寬度 選擇自 CoolSlob 的 Blog
procedure AutoFitable(AGrids: 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;

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/wwcz898/archive/2006/05/08/712990.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章