Home>>Programming>>C++Builder>> » Follow Me to Use the Brushes of a Canvas to Draw Better Graphics in CBuilder

Follow Me to Use the Brushes of a Canvas to Draw Better Graphics in CBuilder

Last Updated on August 9, 2018 By hcyaobin

What’s the brush and how to use the brushes?

The Brush property of a canvas controls the way you fill areas, including the interior of shapes. Filling an area with a brush is a way of changing a large number of adjacent pixels in a specified way.

The brush has three properties you can manipulate:

  1. Color property changes the fill color.
  2. Style property changes the brush style.
  3. Bitmap property uses a bitmap as a brush pattern.

The values of these properties determine the way the canvas fills shapes or other areas. By default, every brush starts out white, with a solid style and no pattern bitmap.

You can use TBrushRecall for quick saving off and restoring the properties of brushes.

How to change the color of the brush?

A brush’s color determines what color the canvas uses to fill shapes. To change the fill color, assign a value to the brush’s Color property. Brush is used for background color in text and line drawing so you typically set the background color property.

You can set the brush color just as you do the pen color, in response to a click on a color grid on the brush’s toolbar :

void __fastcallTForm1::BrushColorClick(TObject *Sender)
{
Canvas->Brush->Color = BrushColor->BackgroundColor;
}

How to change the style of the brush?

A brush style determines what pattern the canvas uses to fill shapes. It lets you specify various ways to combine the brush’s color with any colors already on the canvas. The predefined styles include solid color, no color, and various line and hatch patterns.

To change the style of a brush, set its Style property to one of the predefined values: bsSolid, bsClear, bsHorizontal, bsVertical, bsFDiagonal, bsBDiagonal, bsCross, or bsDiagCross.

This example sets brush styles by sharing a click-event handler for a set of eight brush-style buttons. All eight buttons are selected, the Object Inspector|Events|OnClick is set, and the OnClick handler is named SetBrushStyle. Here is the handler code:

void __fastcallTForm1::SetBrushStyle(TObject *Sender)
{
if (Sender == SolidBrush)
Canvas->Brush->Style = bsSolid;
else if (Sender == ClearBrush)
Canvas->Brush->Style = bsClear;
else if (Sender == HorizontalBrush)
Canvas->Brush->Style = bsHorizontal;
else if (Sender == VerticalBrush)
Canvas->Brush->Style = bsVertical;
else if (Sender == FDiagonalBrush)
Canvas->Brush->Style = bsFDiagonal;
else if (Sender == BDiagonalBrush)
Canvas->Brush->Style = bsBDiagonal;

else if (Sender == CrossBrush)
Canvas->Brush->Style = bsCross;
else if (Sender == DiagCrossBrush)
Canvas->Brush->Style = bsDiagCross;
}

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

void __fastcallTForm1::SetBrushStyle(TObject *Sender)
{
if (Sender->InheritsFrom (__classid(TSpeedButton))
Canvas->Brush->Style = (TBrushStyle) ((TSpeedButton *)Sender)->Tag;
}

How to set the bitmap property and use the FillRect method to draw a bitmap?

A brush’s Bitmap property lets you specify a bitmap image for the brush to use as a pattern for filling shapes and other areas.

The following example loads a bitmap from a file and assigns it to the Brush of the Canvas of Form1:

BrushBmp->LoadFromFile(“MyBitmap.bmp”);

Form1->Canvas->Brush->Bitmap = BrushBmp;
Form1->Canvas->FillRect(Rect(0,0,100,100));

Note: The brush does not assume ownership of a bitmap object assigned to its Bitmap property. You must ensure that the Bitmap object remains valid for the lifetime of the Brush, and you must free the Bitmap object yourself afterwards.

How to read and set the pixels of a canvas?

You will notice that every canvas has an indexed Pixels property that represents the individual colored points that make up the image on the canvas. You rarely need to access Pixels directly, it is available only for convenience to perform small actions such as finding or setting a pixel’s color.

Note: Setting and getting individual pixels is thousands of times slower than performing graphics operations on regions. Do not use the Pixel array property to access the image pixels of a general array. For high-performance access to image pixels, see the TBitmap::ScanLine property.

Add a Comment

Your email address will not be published. Required fields are marked *