The only real difference between TDrawGrid
and TStringGrid
is that TDrawGrid
does not store any cell data itself whereas TStringGrid
does, and also that you have to draw everything yourself in a TDrawGrid
whereas TStringGrid
default-draws the cell strings for you (but you can also custom draw the cells if desired). You have your own arrays for storing your cell data. Use the OnDrawCell
event to draw the cells however you want. It gives you the Col
and Row
of the cell currently being drawn. You would simply access the corresponding array elements and set the grid's Canvas
properties accordingly, such as its Brush.Color
and Font.Color
, then call the Canvas.FillRect()
and Canvas.TextRect()
methods as needed.
As for handling clicks, all you would do is update your array as needed and then Invalidate()
the grid to trigger a repaint using the latest data.
For example:
procedure TForm1.TimerTick(Sender: TObject);
begin
// update contents of currentArray as needed...
DrawGrid1.Invalidate;
end;
procedure TForm1.DrawGrid1Click(Sender: TObject);
begin
if currentArray[DrawGrid1.Col][DrawGrid1.Row] <> 'T' then
begin
currentArray[DrawGrid1.Col][DrawGrid1.Row] := 'T';
DrawGrid1.Invalidate;
end;
end;
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Longint; Rect: TRect; State: TGridDrawState);
begin
if currentArray[ACol][ARow] = 'F' then
begin
DrawGrid1.Canvas.Brush.Color := clWhite;
DrawGrid1.Canvas.Font.Color := clBlack;
end else
begin
DrawGrid1.Canvas.Brush.Color := clBlack;
DrawGrid1.Canvas.Font.Color := clWhite;
end;
DrawGrid1.Canvas.FillRect(Rect);
DrawGrid1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, currentArray[ACol][ARow]);
end;