Drawing the Component Image

From RAD Studio
Jump to: navigation, search

Go Up to Creating a graphic component Index

The essential element of a graphic control is the way it paints its image on the screen. The abstract type TGraphicControl defines a method called Paint that you override to paint the image you want on your control.

The Paint method for the shape control needs to do several things:

  • Use the pen and brush selected by the user.
  • Use the selected shape.
  • Adjust coordinates so that squares and circles use the same width and height.

Overriding the Paint method requires two steps:

  1. Add Paint to the component's declaration.
  2. Write the Paint method in the implementation part of the unit.

For the shape control, add the following declaration to the class declaration:

type
  TSampleShape = class(TGraphicControl)
  .
  .
  .
  protected
    procedure Paint; override;
  .
  .
  .
  end;
class PACKAGE TSampleShape : public TGraphicControl
{
  .
  .
  .
protected:
    virtual void __fastcall Paint();
    .
    .
    .
};

Then write the method in the implementation part of the unit:

procedure TSampleShape.Paint;
begin
  with Canvas do
  begin
    Pen := FPen;                                              { copy the component's pen }
    Brush := FBrush;                                        { copy the component's brush }
    case FShape of
      sstRectangle, sstSquare:
        Rectangle(0, 0, Width, Height);                    { draw rectangles and squares }
      sstRoundRect, sstRoundSquare:
        RoundRect(0, 0, Width, Height, Width div 4, Height div 4); { draw rounded shapes }
      sstCircle, sstEllipse:
        Ellipse(0, 0, Width, Height);                                { draw round shapes }
    end;
  end;
end;
void __fastcall TSampleShape::Paint()
{
  int X,Y,W,H,S;
  Canvas->Pen = FPen;                                // copy the component's pen
  Canvas->Brush = FBrush;                            // copy the component's brush
  W=Width;                                           // use the component width
  H=Height;                                          // use the component height
  X=Y=0;                                             // save smallest for circles/squares
  if( W<H )
    S=W;
  else
    S=H;
  switch(FShape)
  {
    case sstRectangle:                                // draw rectangles and squares
    case sstSquare:
      Canvas->Rectangle(X,Y,X+W,Y+H);
      break;
    case sstRoundRect:                                // draw rounded rectangles and squares
    case sstRoundSquare:
      Canvas->RoundRect(X,Y,X+W,Y+H,S/4,S/4);
      break;
    case sstCircle:                                   // draw circles and ellipses
    case sstEllipse:
      Canvas->Ellipse(X,Y,X+W,Y+H);
      break;
    default:
      break;
  }
}

Paint is called whenever the control needs to update its image. Controls are painted when they first appear or when a window in front of them goes away. In addition, you can force repainting by calling Invalidate, as the StyleChanged method does.