unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  I, J : Integer;
begin
  with StringGrid1 do
    for I := 0 to ColCount - 1 do
      for J:= 0 to RowCount - 1 do
        Cells[I,J] := IntToStr(Random(2001));
  StringGrid1.SetFocus;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
with Stringgrid1 do
begin
 //rysowaie komórki niezaleznie od wpisanego tekstu
    Canvas.Brush.Color:=clWhite;
    Canvas.FillRect(Rect);
    Canvas.Font.Color:=clBlack;
    Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[0,0]);

 with Rect do
  case ACol of
  1:begin //środkowanie w poziomie dla kolumny o numerze 1
       Canvas.FillRect(Rect);
       Canvas.TextOut(Left+(Right-Left-Canvas.TextWidth(Cells[ACol,ARow]))div 2,
       Top+2,
       Cells[ACol,ARow]);
    end;
  2:begin //środkowanie w poziomie i w pionie dla kolumny o numerze 2
       if ARow=2 then Canvas.Font.Color:=clGreen else Canvas.Font.Color:=clBlack;
       Canvas.FillRect(Rect);
       Canvas.TextOut(Left+(Right-Left-Canvas.TextWidth(Cells[ACol,ARow]))div 2,
          Top+ (Bottom-Top-Canvas.TextHeight(Cells[ACol,ARow]))div
          2,Cells[ACol,ARow]);
    end;
  3:begin //wyrównanie do prawej dla kolumny o numerze 3
       Canvas.FillRect(Rect);
       Canvas.TextOut(Left+(Right-Left-Canvas.TextWidth(Cells[ACol,ARow]))-4,
       Top+2, Cells[ACol,ARow]);
    end;
  4:begin //wyrównanie do prawej i środkowanie w pionie dla kolumny o numerze 4
       Canvas.Font.Name:='Allegro BT';
       Canvas.Font.Color:=clRed;
       Canvas.Font.Size:=16;
       Canvas.FillRect(Rect);
       Canvas.TextOut(Left+(Right-Left-Canvas.TextWidth(Cells[ACol,ARow]))-4,
       Top+ (Bottom-Top-Canvas.TextHeight(Cells[ACol,ARow]))div
       2,Cells[ACol,ARow]);
    end;
  end; //case
  end;//with Stringgrid1
end;

end.
