OnPaint (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

When you do custom drawing, such as the one shown in the code for the Button's OnClick event handler below, this drawing will not be persistent and will be erased the first time the PaintBox component needs to repaint.

Code

procedure TForm1.Button1Click(Sender: TObject);
begin
   PaintBox1.Canvas.Brush.Color := clRed;
   PaintBox1.Canvas.FillRect(PaintBox1.Canvas.ClipRect);
   PaintBox1.Canvas.Brush.Color := clBlue;
   PaintBox1.Canvas.Ellipse(0,0,100,50);
end;
{
To make the drawing persistent, you need to also
include an OnPaint event handler that tells the PaintBox how
to redraw itself when it needs to. In the code below, an
ellipse is drawn every time the PaintBox renders itself,
but the PaintBox only paints the client area of the
control red after the Button is clicked. This client area
only stays red until it is invalidated, but the ellipse
persists. This behavior is different from the TImage
component, because the TImage component maintains an
internal bitmap that stores this drawing information for
you.
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
   PaintBox1.Canvas.Brush.Color := clBlue;
   PaintBox1.Canvas.Ellipse(0,0,100,50);
end;

Uses