Raul
Raul

Reputation: 666

Paint TStringGrid cells from outside OnDrawCell event, is it possible?

Is there any way to paint specific cells on Delphi's TStringGrid without using the OnDrawCell event, for instance if I click a button the specified cells will be painted depending on their content.

Upvotes: 2

Views: 2335

Answers (3)

jpfollenius
jpfollenius

Reputation: 16612

No, that is not possible. The next time Windows decides to redraw the control (something you can't really control), everything you have drawn will be overpainted by the Control's Paint method and all painting-related events.

You have to use the event approach to do custom painting like that as Jeroen points out.

Upvotes: 3

Jeroen Wiert Pluimers
Jeroen Wiert Pluimers

Reputation: 24463

To keep the painting persistent, the way you should do this is as follows:

  • in the button OnClick event handler, set some data that distinguishes these cells
  • in the same event handler, invalidate the painting area of cells
  • in OnDrawCell event handler do a normal painting for the cells not distinguished
  • in the same event handler, paint your distinguished cells differently

--jeroen

Upvotes: 11

SimaWB
SimaWB

Reputation: 9294

procedure TForm1.Button1Click(Sender: TObject);
var aRect: TRect;
begin
  aRect := StringGrid1.CellRect(2,2);

  StringGrid1.Canvas.Brush.Color := clBlue; 
  StringGrid1.Canvas.FillRect(aRect);
  StringGrid1.Canvas.Font.Color := clBlack;
  StringGrid1.Canvas.TextOut(aRect.Left + 2 , aRect.Top + 2, StringGrid1.Cells[2, 2]);
end;

Upvotes: 1

Related Questions