Difference between revisions of "TBrush"

From Lazarus wiki
Jump to navigationJump to search
(Initial commit)
 
(Improved sample code. More details.)
Line 2: Line 2:
  
 
Along with <tt>[[TPen]]</tt> and <tt>[[TFont]]</tt>, '''<tt>TBrush</tt>''' belongs to the most fundamental properties of <tt>[[TCanvas]]</tt> and defines how a geometric shape is filled.
 
Along with <tt>[[TPen]]</tt> and <tt>[[TFont]]</tt>, '''<tt>TBrush</tt>''' belongs to the most fundamental properties of <tt>[[TCanvas]]</tt> and defines how a geometric shape is filled.
 +
 +
__TOC__
  
 
== Properties ==
 
== Properties ==
* '''<tt>Color: TColor</tt>''': specifies the basic color used to fill the shape
+
* '''<tt>Color</tt>''': specifies the basic color (type <tt>TColor</tt>) used to fill the shape
* '''<tt>Style: TBrushStyle</tt>''': defines the pattern used for filling
+
* '''<tt>Style</tt>''': an enumeration of type <tt>TBrushStyle</tt> which defines the pattern used for filling
 
**'''<tt>bsClear</tt>''': no filling at all, <tt>Color</tt> is ignored.
 
**'''<tt>bsClear</tt>''': no filling at all, <tt>Color</tt> is ignored.
 
**'''<tt>bsSolid</tt>''': uniform fill of the shape with the <tt>Color</tt> specified
 
**'''<tt>bsSolid</tt>''': uniform fill of the shape with the <tt>Color</tt> specified
**The following styles define '''hatch patterns''' which are drawn in the <tt>Color</tt> specified. By default the background between the hatch lines is left empty, but it can be filled by calling <syntaxhighlight inline lang="pascal">SetBkColor(Canvas.Handle, AColor)</syntaxhighlight> if background mode has been set to <tt>OPAQUE</tt>: <syntaxhighlight inline lang="Pascal">SetBkMode(Canvas.Handle, OPAQUE)</syntaxhighlight> which is the default. Both functions require the unit ''LCLIntf'' in the ''uses'' clause.
+
**The following styles define '''hatch patterns''' which are drawn in the <tt>Color</tt> specified. By default the background between the hatch lines is left empty, but it can be filled after calling <syntaxhighlight inline lang="pascal">SetBkColor(Canvas.Handle, AColor)</syntaxhighlight> if background mode has been set to <tt>OPAQUE</tt>: <syntaxhighlight inline lang="Pascal">SetBkMode(Canvas.Handle, OPAQUE)</syntaxhighlight> which is the default, but can be reverted by calling with the argument <tt>TRANSPARENT</tt> instead of <tt>OPAQUE</tt>. Both functions require the units ''LCLIntf'' and ''LCLType'' in the ''uses'' clause. Note that an alpha-channel for transparency seems to be ignored.
 
***'''<tt>bsHorizontal</tt>''', '''<tt>bsVertical</tt>''', '''<tt>bsFDiagonal</tt>''', '''<tt>bsBDiagonal</tt>''' define a single-line hatch pattern.
 
***'''<tt>bsHorizontal</tt>''', '''<tt>bsVertical</tt>''', '''<tt>bsFDiagonal</tt>''', '''<tt>bsBDiagonal</tt>''' define a single-line hatch pattern.
 
***'''<tt>bsCross</tt>''' and '''<tt>bsDiagCross</tt>''' define a crossed pattern.
 
***'''<tt>bsCross</tt>''' and '''<tt>bsDiagCross</tt>''' define a crossed pattern.
**'''<tt>bsImage</tt>''' and '''<tt>bsPattern</tt>''' tile the shape with the image set to the '''<tt>Bitmap</tt>''' property of the <tt>Brush</tt>. Any instance of a class descending from <tt>TCustomBitmap</tt> can be used here (<tt>TBitmap</tt>, <tt>TJpegImage</tt>, <tt>TPortableNetworkgraphic</tt>, etc). But note that <tt>TCanvas.Bitmap</tt> is not created and destroyed by the <tt>Canvas</tt>, it is your responsibility to create and destroy it. Note also that there are '''<tt>Image</tt>''' (type <tt>TFPCustomImage</tt>) and '''<tt>Pattern</tt>''' (type <tt>TBrushPattern</tt>) properties which seem to have a similar purpose, but they are inherited from the <tt>Brush</tt> ancestor <tt>TFPCustomBrush</tt> and are ignored by <tt>TCanvas</tt>.
+
**'''<tt>bsImage</tt>''' and '''<tt>bsPattern</tt>''' tile the shape with the image set to the '''<tt>Bitmap</tt>''' property of the <tt>Brush</tt>. Any instance of a class descending from <tt>TCustomBitmap</tt> can be used here (<tt>TBitmap</tt>, <tt>TJpegImage</tt>, <tt>TPortableNetworkgraphic</tt>, etc). But note that <tt>TCanvas.Bitmap</tt> is not created and destroyed by the <tt>Canvas</tt>, it is your responsibility to take care of this. Note also that there are also '''<tt>Image</tt>''' (type <tt>TFPCustomImage</tt>) and '''<tt>Pattern</tt>''' (type <tt>TBrushPattern</tt>) properties which seem to have a similar purpose, but they are inherited from the <tt>Brush</tt> ancestor <tt>[[TFPCustomBrush]]</tt> and are ignored by <tt>TCanvas</tt>.
  
  
 
== Sample code ==
 
== Sample code ==
[[image:tbrush_style.png]]
+
[[image:tbrush_style_bk.png]]
  
 
The following project draws ten rectangles on the canvas of a <tt>TForm</tt>, fills them with each style and labels them with the appropriate style name. The result is shown in the image above.
 
The following project draws ten rectangles on the canvas of a <tt>TForm</tt>, fills them with each style and labels them with the appropriate style name. The result is shown in the image above.
Line 30: Line 32:
  
 
uses
 
uses
   ...,  
+
   ...,
   Types,   // for OffsetRect
+
  LCLIntf, LCLType,  // for SetBkColor, OPAQUE and TRANSPARENT
   TypInfo; // for GetEnumName
+
   Types,             // for OffsetRect
 +
   TypInfo;           // for GetEnumName
  
 
procedure TForm1.FormCreate(Sender: TObject);
 
procedure TForm1.FormCreate(Sender: TObject);
Line 40: Line 43:
  
 
   FPattern := TPortableNetworkGraphic.Create;
 
   FPattern := TPortableNetworkGraphic.Create;
   FPattern.LoadFromFile('C:\Lazarus\images\actions\laz_open.png');  // path to the image used for filling
+
   FPattern.LoadFromFile('C:\Lazarus\images\debugger\debugger.png');  // path to the image used for filling
 
end;
 
end;
  

Revision as of 16:53, 24 November 2023

Template:TBrush

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

Properties

  • Color: specifies the basic color (type TColor) used to fill the shape
  • Style: an enumeration of type TBrushStyle which defines the pattern used for filling
    • bsClear: no filling at all, Color is ignored.
    • bsSolid: uniform fill of the shape with the Color specified
    • The following styles define hatch patterns which are drawn in the Color specified. By default the background between the hatch lines is left empty, but it can be filled after calling SetBkColor(Canvas.Handle, AColor) if background mode has been set to OPAQUE: SetBkMode(Canvas.Handle, OPAQUE) which is the default, but can be reverted by calling with the argument TRANSPARENT instead of OPAQUE. Both functions require the units LCLIntf and LCLType in the uses clause. Note that an alpha-channel for transparency seems to be ignored.
      • bsHorizontal, bsVertical, bsFDiagonal, bsBDiagonal define a single-line hatch pattern.
      • bsCross and bsDiagCross define a crossed pattern.
    • bsImage and bsPattern tile the shape with the image set to the Bitmap property of the Brush. Any instance of a class descending from TCustomBitmap can be used here (TBitmap, TJpegImage, TPortableNetworkgraphic, etc). But note that TCanvas.Bitmap is not created and destroyed by the Canvas, it is your responsibility to take care of this. Note also that there are also Image (type TFPCustomImage) and Pattern (type TBrushPattern) properties which seem to have a similar purpose, but they are inherited from the Brush ancestor TFPCustomBrush and are ignored by TCanvas.


Sample code

tbrush style bk.png

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.

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
    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;
    Canvas.Brush.Style := bsClear;  // No text background color
    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;