Skip to content

Instantly share code, notes, and snippets.

@auycro
Created February 3, 2015 03:40
Show Gist options
  • Save auycro/03799b6129d80774b287 to your computer and use it in GitHub Desktop.
Save auycro/03799b6129d80774b287 to your computer and use it in GitHub Desktop.
DrawMultipleLine in TStringGrid Delphi
//グリッドのセルを作成
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if Pos(#13#10, StringGrid1.Cells[ACol, ARow]) > 0 then
begin
StringGrid1.Canvas.FillRect(Rect);
Inc(Rect.Left, 2);
Inc(Rect.Top, 2);
DrawText(StringGrid1.Canvas.Handle, PChar(StringGrid1.Cells[ACol, ARow]), -1, Rect, DT_NOPREFIX or DT_WORDBREAK);
//OutputDebugString(PChar(StringGrid1.Cells[ACol, ARow]));
end;
end;
//セルの高さ更新
procedure TForm1.UpdateRowHeights(AGrid: TStringGrid);
var
Y: Integer;
MaxHeight: Integer;
X: Integer;
R: TRect;
TxtHeight: Integer;
begin
for Y := AGrid.FixedRows to AGrid.RowCount - 1 do
begin
MaxHeight := AGrid.DefaultRowHeight - 4;
for X := AGrid.FixedCols to AGrid.ColCount - 1 do
begin
R := Rect(0, 0, AGrid.ColWidths[X] - 4, 0);
TxtHeight := DrawText(AGrid.Canvas.Handle, PChar(AGrid.Cells[X, Y]), -1,
R, DT_WORDBREAK or DT_CALCRECT);
if TxtHeight > MaxHeight then
MaxHeight := TxtHeight;
end;
AGrid.RowHeights[Y] := MaxHeight + 4;
end;
end;
//グリッドを再作成
procedure TForm1.RefreshStringGrid;
begin
UpdateRowHeights(StringGrid1);
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment