unit uMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TMainForm = class(TForm)
DrawGrid1: TDrawGrid;
DrawGrid2: TDrawGrid;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure DrawGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button1Click(Sender: TObject);
procedure DrawGridMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
procedure RepaintCells;
end;
var
MainForm: TMainForm;
Colors1,Colors2: array of array of TColor;
implementation
{$R *.dfm}
procedure TMainForm.RepaintCells;
var
cols,rows: Integer;
i,j: Integer;
begin
Randomize;
cols := DrawGrid1.ColCount;
rows := DrawGrid1.RowCount;
SetLength(Colors1,cols,rows);
for i := 0 to cols-1 do
begin
for j := 0 to rows-1 do
begin
if ((Random(100) mod 2) = 0) then
Colors1[i,j] := clNavy
else
Colors1[i,j] := clWindow;
end;
end;
cols := DrawGrid2.ColCount;
rows := DrawGrid2.RowCount;
SetLength(Colors2,cols,rows);
for i := 0 to cols-1 do
begin
for j := 0 to rows-1 do
begin
if ((Random(100) mod 2) = 0) then
Colors2[i,j] := clNavy
else
Colors2[i,j] := clWindow;
end;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
RepaintCells;
end;
procedure TMainForm.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
TDrawGrid(Sender).Canvas.Brush.Color := Colors1[ACol, ARow];
TDrawGrid(Sender).Canvas.FillRect(Rect);
end;
procedure TMainForm.DrawGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
TDrawGrid(Sender).Canvas.Brush.Color := Colors2[ACol, ARow];
TDrawGrid(Sender).Canvas.FillRect(Rect);
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
RepaintCells;
DrawGrid1.Repaint;
DrawGrid2.Repaint;
end;
procedure TMainForm.DrawGridMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
col,row: Integer;
begin
TDrawGrid(Sender).MouseToCell(X,Y,col,row);
if (col < 0) or (row < 0) then
Exit;
if (clWindow <> Colors1[col,row]) and (Colors1[col,row] = Colors2[col,row]) then
begin
Colors1[col,row] := clWindow;
DrawGrid1.Repaint;
end;
end;
end.