bestlong 怕失憶論壇
標題:
使StringGrid自動依內容調整欄位寬度
[打印本頁]
作者:
bestlong
時間:
2008-10-24 14:30
標題:
使StringGrid自動依內容調整欄位寬度
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
MaxWidth,MaxHeight: integer;
begin
//自動調整寬度
MaxWidth := StringGrid1.Canvas.TextWidth(StringGrid1.Cells[ACol, ARow]);
if MaxWidth > StringGrid1.ColWidths[ACol] then
StringGrid1.ColWidths[ACol] := MaxWidth + 5;
//自動調整高度
MaxHeight := StringGrid1.Canvas.TextHeight(StringGrid1.Cells[ACol, ARow]);
if MaxHeight > StringGrid1.RowHeights[ARow] then
StringGrid1.RowHeights[ARow] := MaxHeight + 4;
end;
複製代碼
作者:
bestlong
時間:
2008-10-24 14:31
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;
複製代碼
歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/)
Powered by Discuz! X1.5