例:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (acol=1) and (arow=2) then
begin
StringGrid1.Canvas.Font.Color:=clred;
StringGrid1.Canvas.Brush.Color:=clyellow;
StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,StringGrid1.Cells[Acol,arow]);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
i,j : integer;
begin
StringGrid1.RowCount:=5;
StringGrid1.ColCount:=5;
for i:=0 to 4 do
for j:= 0 to 4 do
StringGrid1.Cells[i,j]:=inttostr(i*j);
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (acol=1) and (arow=2) then
begin
StringGrid1.Canvas.Font.Color:=clred;
StringGrid1.Canvas.Brush.Color:=clyellow;
StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,StringGrid1.Cells[Acol,arow]);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
i,j : integer;
begin
StringGrid1.RowCount:=5;
StringGrid1.ColCount:=5;
for i:=0 to 4 do
for j:= 0 to 4 do
StringGrid1.Cells[i,j]:=inttostr(i*j);
end;end.
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
Txt:String;
begin
if (ACol=1) and (ARow=2) then
begin
Txt:=StringGrid1.Cells[ACol,ARow];
StringGrid1.Canvas.Font.Color:=clred;
StringGrid1.Canvas.Brush.Color:=clyellow;
StringGrid1.Canvas.Rectangle(Rect);
DrawText(StringGrid1.Canvas.Handle,
PChar(Txt),
Length(Txt),
Rect,
DT_Center or DT_VCenter or DT_SingleLine
);
end;
end;