Changing the Brush Style
Go Up to Using Brushes
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: bsBDiagonal, bsClear, bsCross, bsDiagCross, bsFDiagonal, bsHorizontal, bsSolid, or bsVertical.
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:
procedure TForm1.SetBrushStyle(Sender: TObject);
begin
with Canvas.Brush do
begin
if Sender = SolidBrush then Style := bsSolid
else if Sender = ClearBrush then Style := bsClear
else if Sender = HorizontalBrush then Style := bsHorizontal
else if Sender = VerticalBrush then Style := bsVertical
else if Sender = FDiagonalBrush then Style := bsFDiagonal
else if Sender = BDiagonalBrush then Style := bsBDiagonal
else if Sender = CrossBrush then Style := bsCross
else if Sender = DiagCrossBrush then Style := bsDiagCross;
end;
end;
void __fastcall TForm1::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 __fastcall TForm1::SetBrushStyle(TObject *Sender) {
if (Sender->InheritsFrom(__classid(TSpeedButton)) Canvas->Brush->Style =
(TBrushStyle)((TSpeedButton*)Sender)->Tag;}