Programming C++Builder Follow Me to Use the Pen of a Canvas to Draw Graphics in CBuilder

Pen and Brush in TCanvas

TCanvas is the base class that you can use to draw on it. In this article, you would learn how to use the pen to make some basic pictures on a canvas.

With the Canvas object, you can set the properties of a pen for drawing lines, a brush for filling shapes, a font for writing text, and an array of pixels to represent the image.

This section describes:

  1. Using pens.
  2. Using brushes.
  3. Reading and setting pixels.

How to use Pen?

The Pen property of a canvas controls the way lines appear, including lines drawn as the outlines of shapes. Drawing a straight line is really just changing a group of pixels that lie between two points.

The pen itself has four properties you can change: Color, Width, Style, and Mode.

  1. Color property changes the pen color.
  2. Width property changes the pen width.
  3. Style property changes the pen style.
  4. Mode property changes the pen mode.
  5. PenPos property getting the current drawing position.

The values of these properties determine how the pen changes the pixels in the line. By default, every pen starts out black, with a width of 1 pixel, a solid style, and a mode called copy that overwrites anything already on the canvas.

You can use TPenRecall for quick saving off and restoring the properties of pens.

How to change the colors of pen?

You can set the color of a pen as you would any other Color property at runtime. A pen's color determines the color of the lines the pen draws, including lines drawn as the boundaries of shapes, as well as other lines and polylines. To change the pen color, assign a value to the Color property of the pen.

To let the user choose a new color for the pen, put a color grid on the pen's toolbar. A color grid can set both foreground and background colors. For a non-grid pen style, you must consider the background color, which is drawn in the gaps between line segments. Background color comes from the Brush color property.

Since the user chooses a new color by clicking the grid, this code changes the pen's color in response to the OnClick event:


void __fastcallTForm1::PenColorClick(TObject *Sender)
  Canvas->Pen->Color = PenColor->ForegroundColor;


How to change the width of pen?

A pen's width determines the thickness, in pixels, of the lines it draws.

Note: When the thickness is greater than 1, Windows always draw solid lines, regardless of the value of the pen's Style property.

To change the pen width, assign a numeric value to the pen's Width property.

Suppose you have a scroll bar on the pen's toolbar to set width values for the pen. And suppose you want to update the label next to the scroll bar to provide feedback to the user. Using the scroll bar's position to determine the pen width, you update the pen width every time the position changes.

This is how to handle the scroll bar's OnChange event:


void __fastcallTForm1::PenWidthChange(TObject *Sender)
  Canvas->Pen->Width = PenWidth->Position;         // set the pen width directly
  PenSize->Caption = IntToStr(PenWidth->Position); // convert to string


How to change the style of pen?

A pen's Style property allows you to set solid lines, dashed lines, dotted lines, and so on.

VCL Note: For cross-platform applications deployed under Windows, Windows does not support dashed or dotted line styles for pens wider than one pixel and makes all larger pens solid, no matter what style you specify.

The task of setting the properties of pen is an ideal case for having different controls share same event handler to handle events. To determine which control actually got the event, you check the Sender parameter.

To create one click-event handler for six pen-style buttons on a pen's toolbar, do the following:

  1. Select all six pen-style buttons and select the Object Inspector|Events|OnClick event and in the Handler column, type SetPenStyle.
    C++Builder generates an empty click-event handler called SetPenStyle and attaches it to the OnClick events of all six buttons.
  2. Fill in the click-event handler by setting the pen's style depending on the value of Sender, which is the control that sent the click event:


void __fastcallTForm1::SetPenStyle(TObject *Sender)
  if (Sender == SolidPen)     Canvas->Pen->Style = psSolid;   else if (Sender == DashPen)     Canvas->Pen->Style = psDash;   else if (Sender == DotPen)     Canvas->Pen->Style = psDot;   else if (Sender == DashDotPen)     Canvas->Pen->Style = psDashDot;   else if (Sender == DashDotDotPen)     Canvas->Pen->Style = psDashDotDot; else if (Sender == ClearPen)     Canvas->Pen->Style = psClear; }


The above event handler code could be further reduced by putting the pen style constants into the Tag properties of the pen style buttons. Then this event code would be something like:


void __fastcallTForm1::SetPenStyle(TObject *Sender)
  if (Sender->InheritsFrom (__classid(TSpeedButton)) 
    Canvas->Pen->Style = (TPenStyle) ((TSpeedButton *)Sender)->Tag; }


How to get the pen mode?

A pen's Mode property lets you specify various ways to combine the pen's color with the color on the canvas. For example, the pen could always be black, be an inverse of the canvas background color, inverse of the pen color, and so on.

How to get the current position of the pen?

The current drawing position--the position from which the pen begins drawing its next line--is called the pen position. The canvas stores its pen position in its PenPos property. Pen position affects the drawing of lines only; for shapes and text, you specify all the coordinates you need.

To set the pen position, call the MoveTo method of the canvas. For example, the following code moves the pen position to the upper left corner of the canvas:


Canvas->MoveTo(0, 0);


Note: Drawing a line with the LineTo method also moves the current position to the endpoint of the line.

Add comment

Security code

Programming - C++Builder