Автоматический подбор ширины колонок для TStringGrid
Оформил: DeeCo
Автор: http://www.swissdelphicenter.ch
procedure SetGridColumnWidths(Grid: TStringGrid; const Columns: array of Integer); { When you double-Click on a Column-Header the Column autosizes to fit its content Bei Doppelklick auf eine fixierte Spalte passt sich die Spaltenbreite der Textgrosse an } procedure AutoSizeGridColumn(Grid: TStringGrid; column, min, max: Integer); { Set for max and min some minimal/maximial Values} { Bei max and min kann eine Minimal- resp. Maximalbreite angegeben werden} var i: Integer; temp: Integer; tempmax: Integer; begin tempmax := 0; for i := 0 to (Grid.RowCount - 1) do begin temp := Grid.Canvas.TextWidth(Grid.cells[column, i]); if temp > tempmax then tempmax := temp; if tempmax > max then begin tempmax := max; break; end; end; if tempmax < min then tempmax := min; Grid.ColWidths[column] := tempmax + Grid.GridLineWidth + 3; end; procedure TForm1.StringGrid1DblClick(Sender: TObject); var P: TPoint; iColumn, iRow: Longint; begin GetCursorPos(P); with StringGrid1 do begin P := ScreenToClient(P); MouseToCell(P.X, P.Y, iColumn, iRow); if P.Y < DefaultRowHeight then AutoSizeGridColumn(StringGrid1, iColumn, 40, 100); end; end;
Далее: Автоматический подбор ширины колонок для TStringGrid 2 »»