unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, Controls, Graphics, Grids, Types;
type
{ TForm1 }
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
with StringGrid1 do begin
DefaultColWidth := 100; // to customize fixed column's width
Clear;
// Add 4 columns
Columns.Add;
Columns.Add;
Columns.Add;
Columns.Add;
// Set the width
Columns[0].Width := 100;
Columns[1].Width := 50;
Columns[2].Width := 100;
Columns[3].Width := 100;
// Set the alignments
Columns[0].Alignment := taRightJustify;
Columns[1].Alignment := taRightJustify;
Columns[2].Alignment := taCenter;
Columns[3].Alignment := taLeftJustify;
// Set the header's alignments
Columns[0].Title.Alignment := taCenter;
Columns[1].Title.Alignment := taCenter;
Columns[2].Title.Alignment := taCenter;
Columns[3].Title.Alignment := taCenter;
// Set the header's texts
Columns[0].Title.Caption := 'A';
Columns[1].Title.Caption := 'B';
Columns[2].Title.Caption := 'C';
Columns[3].Title.Caption := 'D';
// Put some data
Cells[0, 1] := 'one'; Cells[1, 1] := '123'; Cells[2, 1] := '1'; Cells[3, 1] := 'tokyo'; Cells[4, 1] := 'leopard';
Cells[0, 2] := 'two'; Cells[1, 2] := '80'; Cells[2, 2] := '2'; Cells[3, 2] := 'mexico'; Cells[4, 2] := 'dog';
Cells[0, 3] := 'three'; Cells[1, 3] := '2048'; Cells[2, 3] := '3'; Cells[3, 3] := 'paris'; Cells[4, 3] := 'cat';
Cells[0, 4] := 'four'; Cells[1, 4] := '7'; Cells[2, 4] := '4'; Cells[3, 4] := 'beijing'; Cells[4, 4] := 'fish';
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
var
TextStyle : TTextStyle;
Pos : TRect;
begin
if aCol <> 0 then Exit;
StringGrid1.Canvas.Brush.Color := cl3DFace;
StringGrid1.Canvas.FillRect(aRect);
StringGrid1.Canvas.Pen.Color := clInfoText;
TextStyle.Alignment := taRightJustify;
TextStyle.Layout := tlBottom;
TextStyle.SingleLine := True;
TextStyle.Clipping := True;
TextStyle.ExpandTabs := True;
TextStyle.ShowPrefix := True;
TextStyle.Wordbreak := True;
TextStyle.Opaque := False;
TextStyle.SystemFont := False;
TextStyle.RightToLeft := False;
TextStyle.EndEllipsis := False;
Pos := aRect;
Dec(Pos.Bottom, 4);
Dec(Pos.Right, 5);
StringGrid1.Canvas.TextRect(Pos, 0, 10, StringGrid1.Cells[aCol, aRow], TextStyle);
end;
end.