unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
StdCtrls, Types;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
procedure StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
var CanSelect: Boolean);
private
public
end;
var
Form1: TForm1;
implementation
const
N : array[0..10,0..10] of byte =
((0,1,2,3,4,4,3,2,1,1,0),
(1,0,1,3,4,4,4,3,2,1,0),
(2,1,0,2,3,4,5,4,3,2,0),
(3,3,2,0,2,4,5,6,5,3,1),
(4,4,3,2,0,2,5,7,7,5,2),
(4,4,4,4,2,0,3,7,9,8,4),
(3,4,5,5,5,3,0,5,9,11,8),
(2,3,4,6,7,7,5,0,8,15,15),
(1,2,3,5,7,9,9,8,0,15,30),
(1,1,2,3,5,8,11,15,15,0,61),
(0,0,0,1,2,4,8,15,30,61,0));
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
i, j : byte;
begin
// load numbers into grid
for i := 0 to 10 do
for j := 0 to 10 do
StringGrid1.Cells[i,j] := IntToStr(N[i,j]);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
StringGrid1.Row:=4;
StringGrid1.Col:=4;
StringGrid1.SetFocus;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
var
r, c : byte;
begin
// set background/font colours
for r := 0 to 10 do
for c := r + 1 to 10 do
if (aCol = c) and (aRow = r) then
with TStringGrid(sender) do begin
Canvas.Brush.Color:= clRed;
canvas.Font.Color:= clWhite;
canvas.FillRect(aRect);
Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);
end;
for c := 0 to 10 do
for r := c + 1 to 10 do
if (aCol = c) and (aRow = r) then
with TStringGrid(sender) do begin
Canvas.Brush.Color:= clBlack;
canvas.Font.Color:= clWhite;
canvas.FillRect(aRect);
Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);
end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
var CanSelect: Boolean);
begin
StringGrid1.SelectedColor:= clBlue;
StringGrid1.Options := StringGrid1.Options + [goDrawFocusSelected];
end;
end.