Home > Net >  Fill inside a rectangle with a pattern Delphi
Fill inside a rectangle with a pattern Delphi

Time:04-29

Using the following code in delphi-firemonkey, it is easy to fill a rectangle with a solid or gradient color, but I cannot figure out how to fill it with a pattern like dots or slant dashes.

FillRect(RectF(a,b, c,d), 70, tbrush.Create(TBrushKind.Solid,TAlphaColorRec.gray));

appreciate the help.

CodePudding user response:

You can use a bitmap to provide the "dots and slants" or any figure to use as the pattern. In this case you define TBrushKind.Bitmap when you create the brush.

Here is an example that keeps the pattern brush as part of the form. The comments explain the details.

  private
    Brush: TBrush;
    BrushBmp: TBrushBitmap;

procedure TForm48.FormCreate(Sender: TObject);
begin
  BrushBmp := TBrushBitmap.Create;
  // Load bitmap to be used as brush
  BrushBmp.Bitmap.LoadFromFile('C:\tmp\BrushA.bmp');
  // Create the brush of proper TBrushKind
  Brush := TBrush.Create(TBrushKind.Bitmap, TAlphaColorRec.Aqua);
  // Assign the bitmap to the brush
  Brush.Bitmap := BrushBmp;
end;

procedure TForm48.FormDestroy(Sender: TObject);
begin
  Brush.Free;
end;

procedure TForm48.Rectangle1Paint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
begin
  // use the bitmap brush
  Canvas.FillRect(ARect, 0.0, 0.0, AllCorners, 1.0, Brush, TCornerType.Bevel);
end;

The bitmap:

enter image description here

And when used as a fill:

enter image description here

  • Related