RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TPaintBox.OnPaint Event

OnPaint occurs when the paintbox receives a Windows paint message.

Pascal
property OnPaint: TNotifyEvent;
C++
__property TNotifyEvent OnPaint;

Write an OnPaint event handler to draw the image of the paint box. The OnPaint event fires when the system repaints the TPaintBox control. If there is a handler for this event, its code is executed when the OnPaint event fires. 

Use properties and methods of the canvas (such as LineTo, Draw, and TextOut) to programmatically construct the image that the paintbox presents on a form. This can consist of a number of operations including drawing lines, images, and shapes. A TPaintBox itself has no visible manifestation, so if an OnPaint event handler is not provided or no drawing is done in the event handler, the paintbox can not be seen on the form at runtime.  

C++ Examples: 

 

/*
When you do custom drawing, such as 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.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
   PaintBox1->Canvas->Brush->Color = clRed;
   PaintBox1->Canvas->FillRect(PaintBox1->Canvas->ClipRect);
   PaintBox1->Canvas->Ellipse(0,0,100,100);
}

/*
In order for the drawing to be 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 will be drawn everytime the PaintBox renders itself,
but the PaintBox will only paint the client area of the
control red after the Button is clicked. This client area
will only stay red until it is invalidated, but the ellipse
will persist. This behavior is different from the TImage
component, because the TImage component maintains an
internal bitmap that stores this drawing information for
you.
*/
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
   PaintBox1->Canvas->Brush->Color = clBlue;
   PaintBox1->Canvas->Ellipse(0,0,100,100);
}
/*
This example paints a white five-pointed star in a paintbox 
control:
*/
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
  TPaintBox *pPB = (TPaintBox *)Sender;
  TPoint points[6];
  pPB->Canvas->Pen->Color = clWhite;
  points[0].x = 40;
  points[0].y = 10;
  points[1].x = 20;
  points[1].y = 60;
  points[2].x = 70;
  points[2].y = 30;
  points[3].x = 10;
  points[3].y = 30;
  points[4].x = 60;
  points[4].y = 60;
  points[5].x = 40;
  points[5].y = 10;
  pPB->Canvas->Polyline(points,5);
}

 

Delphi Examples: 

{
When you do custom drawing, such as 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.
}
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;
{
In order for the drawing to be 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 will be drawn everytime the PaintBox renders itself,
but the PaintBox will only paint the client area of the
control red after the Button is clicked. This client area
will only stay red until it is invalidated, but the ellipse
will persist. 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;
{
This example paints a white five-pointed star in a paintbox 
control:
} 
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  with Sender as TPaintBox do
  begin
    Canvas.Pen.Color := clWhite;
    Canvas.Polyline([Point(40, 10), Point(20, 60), Point(70, 30),
    Point(10, 30), Point(60, 60), Point(40, 10)]);
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!