Sign in Join
Wiki copytemporary

Revision 1 of 2, by Wp on 11-24-2023. The page as it is now.

TBrush

Along with TPen and TFont, TBrush belongs to the most fundamental properties of TCanvas and defines how a geometric shape is filled.

Properties

These properties are applied in all subsequent drawing commands.

Above description is valid for Windows. Several restrictions may apply for other widgetsets.

Sample code

<figure> <img src="tbrush_style_bk.png" title="tbrush_style_bk.png" /> <figcaption>tbrush_style_bk.png</figcaption> </figure>

The following project draws ten rectangles on the canvas of a TForm, fills them with each style and labels them with the appropriate style name. The result is shown in the image above.

pascal
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    FPattern: TCustomBitmap;
  public
  end;

uses
  ...,
  LCLIntf, LCLType,  // for SetBkColor, OPAQUE and TRANSPARENT 
  Types,             // for OffsetRect
  TypInfo;           // for GetEnumName

procedure TForm1.FormCreate(Sender: TObject);
begin
  Width := 550;
  Height := 290;

  FPattern := TPortableNetworkGraphic.Create;
  FPattern.LoadFromFile('C:\Lazarus\images\debugger\debugger.png');  // path to the image used for filling
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FPattern.Free;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  R: TRect;
  bs: TBrushStyle;
  styleName: String;
  x, y, h: Integer;
begin
  R := Rect(8, 8, 108, 108);
  h := Canvas.TextHeight('Tg');

  // Set fill color
  Canvas.Brush.Color := clRed;

  // Set border color
  Canvas.Pen.Color := clBlack;

  // Iterate over all available brush styles
  for bs in TBrushStyle do
  begin
    // Set background color
    SetBkMode(Canvas.Handle, OPAQUE);
    SetBkColor(Canvas.Handle, clYellow); 
    // Set brush style
    Canvas.Brush.Style := bs;
    if (bs = bsImage) or (bs =bsPattern) then
      Canvas.Brush.Bitmap := FPattern
    else
      Canvas.Brush.Bitmap := nil;
    // Fill a rectangle with the selected brush
    Canvas.Rectangle(R);
    // Get name of brush style and draw it below the rectangle
    styleName := GetEnumName(TypeInfo(TBrushStyle), ord(bs));
    x := (R.Left + R.Right - Canvas.TextWidth(styleName)) div 2;
    y := R.Bottom + 8;
    // No text background
    SetBkMode(Canvas.Handle, TRANSPARENT);    // or: Canvas.Brush.Style := bsClear; 
    // Draw the brush name
    Canvas.TextOut(x, y, styleName);
    // Position the rectangle for next brush
    if ord(bs) = ord(High(bs)) div 2 then
      OffsetRect(R, -R.Left + 8, R.Height + 20 + h)
    else
      OffsetRect(R, R.Width + 8, 0);
  end;
end;

From the Free Pascal wiki, page "TBrush", as it stood on 2023-11-24 after a change by Wp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.