Sign in Join
Wiki copytemporary

TBrush: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Along with [[TPen]] and [[TFont]], **`TBrush`** belongs to the most fundamental properties of [[TCanvas]] and defines how a geometric shape is filled.
22
33## Properties
44
55- **`Color`**: specifies the basic color (type `TColor`) used to fill the shape
66- **`Style`**: an enumeration of type `TBrushStyle` which defines the pattern used for filling
77 - **`bsClear`**: no filling at all, `Color` is ignored.
88 - **`bsSolid`**: uniform fill of the shape with the `Color` specified
99 - The following styles define **hatch patterns** which are drawn in the `Color` specified. By default the background between the hatch lines is left empty. In Windows, gtk2 and qt5, 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. In other widgetsets the background has to be drawn twice, with `fsSolid` at first. Note that an alpha-channel for transparency seems to be ignored.
1010 - **`bsHorizontal`**, **`bsVertical`**, **`bsFDiagonal`**, **`bsBDiagonal`** define a single-line hatch pattern.
1111 - **`bsCross`** and **`bsDiagCross`** define a crossed pattern.
1212 - **`bsImage`** and **`bsPattern`** tile the shape with the image set to the **`Bitmap`** property of the `Brush`.
1313- **`Bitmap`** defines the image which fills the shape by tiling if `Style` is `bsImage`. 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. There are also **`Image`** (type `TFPCustomImage`) and **`Pattern`** (type `TBrushPattern`) properties which seem to have a similar purpose, but they are inherited from ancestor [[TFPCustomBrush]] and are ignored by `TCanvas`. Using a bitmap for filling the shape does not work on all widgetsets; at the time of this writing it was tested to work on win32/64, qt5 and cocoa.
1414
1515These properties are applied in all subsequent drawing commands.
1616
1717Above description is valid for Windows. Several restrictions may apply for other widgetsets.
1818
1919## Sample code
2020
21-<figure>
22-<img src="tbrush_style_bk.png" title="tbrush_style_bk.png" />
23-<figcaption>tbrush_style_bk.png</figcaption>
24-</figure>
21+![](/wiki-images/5/53/Tbrush_style_bk.png)
2522
2623The 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.
2724
2825``` pascal
2926type
3027 TForm1 = class(TForm)
3128 procedure FormCreate(Sender: TObject);
3229 procedure FormDestroy(Sender: TObject);
3330 procedure FormPaint(Sender: TObject);
3431 private
3532 FPattern: TCustomBitmap;
3633 public
3734 end;
3835
3936uses
4037 ...,
4138 LCLIntf, LCLType, // for SetBkColor, OPAQUE and TRANSPARENT
4239 Types, // for OffsetRect
4340 TypInfo; // for GetEnumName
4441
4542procedure TForm1.FormCreate(Sender: TObject);
4643begin
4744 Width := 550;
4845 Height := 290;
4946
5047 FPattern := TPortableNetworkGraphic.Create;
5148 FPattern.LoadFromFile('C:\Lazarus\images\debugger\debugger.png'); // path to the image used for filling
5249end;
5350
5451procedure TForm1.FormDestroy(Sender: TObject);
5552begin
5653 FPattern.Free;
5754end;
5855
5956procedure TForm1.FormPaint(Sender: TObject);
6057var
6158 R: TRect;
6259 bs: TBrushStyle;
6360 styleName: String;
6461 x, y, h: Integer;
6562begin
6663 R := Rect(8, 8, 108, 108);
6764 h := Canvas.TextHeight('Tg');
6865
6966 // Set fill color
7067 Canvas.Brush.Color := clRed;
7168
7269 // Set border color
7370 Canvas.Pen.Color := clBlack;
7471
7572 // Iterate over all available brush styles
7673 for bs in TBrushStyle do
7774 begin
7875 // Set background color
7976 SetBkMode(Canvas.Handle, OPAQUE);
8077 SetBkColor(Canvas.Handle, clYellow);
8178 // Set brush style
8279 Canvas.Brush.Style := bs;
8380 if (bs = bsImage) or (bs =bsPattern) then
8481 Canvas.Brush.Bitmap := FPattern
8582 else
8683 Canvas.Brush.Bitmap := nil;
8784 // Fill a rectangle with the selected brush
8885 Canvas.Rectangle(R);
8986 // Get name of brush style and draw it below the rectangle
9087 styleName := GetEnumName(TypeInfo(TBrushStyle), ord(bs));
9188 x := (R.Left + R.Right - Canvas.TextWidth(styleName)) div 2;
9289 y := R.Bottom + 8;
9390 // No text background
9491 SetBkMode(Canvas.Handle, TRANSPARENT); // or: Canvas.Brush.Style := bsClear;
9592 // Draw the brush name
9693 Canvas.TextOut(x, y, styleName);
9794 // Position the rectangle for next brush
9895 if ord(bs) = ord(High(bs)) div 2 then
9996 OffsetRect(R, -R.Left + 8, R.Height + 20 + h)
10097 else
10198 OffsetRect(R, R.Width + 8, 0);
10299 end;
103100end;
104101```
105102
106103---
107104*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](/wiki-contact).*

Back to history