본문 바로가기

delphi

Stringgrid의 셀의 폰트와 정렬

반응형
SMALL

Stringgrid의 셀에서 폰트와 가운데 정렬 등을 할 때 아래와 같은 코드를 사용한다.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  S: string;
  Grid: TStringGrid;
  Font: TFont;
  TextWidth: Integer;
begin
  Grid := Sender as TStringGrid;

  // Get cell contents
  S := Grid.Cells[ACol, ARow];

  // Create font
  Font := TFont.Create;
  Font.Size := 12;
  Font.Name := 'Arial';
  if ARow = 0 then
    Font.Style := [fsBold];

  // Center text horizontally and vertically
  TextWidth := Grid.Canvas.TextWidth(S);
  Rect.Left := Rect.Left + (Rect.Right - Rect.Left - TextWidth) div 2;
  Rect.Top := Rect.Top + (Rect.Bottom - Rect.Top - Grid.Canvas.TextHeight(S)) div 2;

  // Draw cell contents
  Grid.Canvas.FillRect(Rect);
  Grid.Canvas.Font := Font;
  Grid.Canvas.TextRect(Rect, Rect.Left, Rect.Top, S);

  Font.Free;
end;
반응형
LIST