Anyone know how to create custom brushes for TShape?

Posted on 2004-10-05
Last Modified: 2010-04-05

Does anyone know how to create a custom brush?
Specifically a dotted line grid brush, preferably faded so that it looks like a faint background grid.
It's just for alignment when users are positioning things.

Question by:nathc

Expert Comment

ID: 12234181
 Canvas.Font.Color := ForegroundColor;
  Canvas.Brush.Bitmap := Bmp;
  SetBKColor(Canvas.Handle, ColorToRGB(BackgroundColor));
LVL 33

Expert Comment

ID: 12234397
hello  nathc,  making a "Grid" brush will require that you draw your own Brush Bitmap, here is some code that will do a custom grid bitmap brush -
this gridBrush function will return the Handle for a system Brush, it has 3 input parameters, the gdColor idthe Grid Color (lines color of brush) , the BkColor is the Background Color of the brush, , the auto parameter is set to  True  if you want this to automaticly get a slightly darker grid line color from the BkColor. . . . .

function gridBrush(gdColor, BkColor: TColor; auto: Boolean = True): THandle;

  function doByte(lim: Integer): Byte;
  if lim < 0 then Result := 0
    else Result := lim and $FF;

brBmp: TBitmap;
BrushLog1: TLogBrush;
brBmp := TBitmap.Create;
brBmp.Canvas.Brush.Color := BkColor;
brBmp.Width := 8;
brBmp.Height := 8;

if Auto then
  gdColor := RGB(doByte(GetRValue(BkColor) - 26),
                 doByte(GetGValue(BkColor) - 26),
                 doByte(GetBValue(BkColor) - 26));

brBmp.Canvas.Pen.Color := gdColor;
BrushLog1.lbStyle := BS_PATTERN;
BrushLog1.lbHatch := brBmp.Handle;
Result := CreateBrushIndirect(BrushLog1);

procedure TForm1.FormCreate(Sender: TObject);
Shape1.Brush.Handle := gridBrush($A000A0, $C0C066);

ask questions if you need more information
LVL 33

Expert Comment

ID: 12234401
you might also try the look of this variation -


Author Comment

ID: 12236072
Hey Slick812,
That's exactly what I'm looking for, thank you :)

I'm interested to know more about the code.
I looked up TLogBrush in Delphi 7 Help but couldn't find it.
Is it a Windows API object?
Also I guess these functions must be Windows API?

If you could explain TLogBrush and the two functions above I would be grateful. Also where can I go to get more information about Windows API functions,classes etc(if indeed these are Windows API).



Expert Comment

ID: 12238223
You can look up the API function at
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

LVL 33

Accepted Solution

Slick812 earned 125 total points
ID: 12242292
yes, the TLogBrush (it's a  LOGBRUSH  structure in the API Help and MS reference), GetBValue( ) and  CreateBrushIndirect( ) are ALL  API system functions, and to find out more information about these, you can look in your API Help (almost all versions of Delphi come with the Win32 API Help, except the downLoaded ones), as pritaeas has said there is so much info at MSDN about this, it is difficult to find what you may be interested in, the addy for it is

you should use their  "Search For" to help you find some info. . .

The  CreateBrushIndirect( )  function uses a LOGBRUSH (TLogBrush in Delphi) to get specs to create a system brush, here is the text copied from API Help about LOGBRUSH - -

The LOGBRUSH structure defines the style, color, and pattern of a physical brush. It is used by the CreateBrushIndirect and ExtCreatePen functions.

typedef struct tagLOGBRUSH { // lb  
   UINT     lbStyle;
   COLORREF lbColor;
   LONG     lbHatch;



Specifies the brush style. The lbStyle member must be one of the following styles:

Value      Meaning
BS_DIBPATTERN      A pattern brush defined by a device-independent bitmap (DIB) specification. If lbStyle is BS_DIBPATTERN, the lbHatch member contains a handle to a packed DIB.Windows 95: Creating brushes from bitmaps or DIBs larger than 8x8 pixels is not supported. If a larger bitmap is given, only a portion of the bitmap is used.
BS_DIBPATTERNPT      A pattern brush defined by a device-independent bitmap (DIB) specification. If lbStyle is BS_DIBPATTERNPT, the lbHatch member contains a pointer to a packed DIB.
BS_HATCHED      Hatched brush.
BS_HOLLOW      Hollow brush.
BS_NULL      Same as BS_HOLLOW.
BS_PATTERN      Pattern brush defined by a memory bitmap.
BS_SOLID      Solid brush.


Specifies the color in which the brush is to be drawn. If lbStyle is the BS_HOLLOW or BS_PATTERN style, lbColor is ignored.

If lbStyle is BS_DIBPATTERN or BS_DIBPATTERNBT, the low-order word of lbColor specifies whether the bmiColors members of the BITMAPINFO structure contain explicit red, green, blue (RGB) values or indices into the currently realized logical palette. The lbColor member must be one of the following values:

Value      Meaning
DIB_PAL_COLORS      The color table consists of an array of 16-bit indices into the currently realized logical palette.
DIB_RGB_COLORS      The color table contains literal RGB values.


Specifies a hatch style. The meaning depends on the brush style defined by lbStyle.

If lbStyle is BS_DIBPATTERN, the lbHatch member contains a handle to a packed DIB. If lbStyle is BS_DIBPATTERNPT, the lbHatch member contains a pointer to a packed DIB.
If lbStyle is BS_HATCHED, the lbHatch member specifies the orientation of the lines used to create the hatch. It can be one of the following values:

Value      Meaning
HS_BDIAGONAL      A 45-degree upward, left-to-right hatch
HS_CROSS      Horizontal and vertical cross-hatch
HS_DIAGCROSS      45-degree crosshatch
HS_FDIAGONAL      A 45-degree downward, left-to-right hatch
HS_HORIZONTAL      Horizontal hatch
HS_VERTICAL      Vertical hatch

If lbStyle is BS_PATTERN, lbHatch is a handle to the bitmap that defines the pattern. The bitmap cannot be a DIB section bitmap, which is created by the CreateDIBSection function.
If lbStyle is BS_SOLID or BS_HOLLOW, lbHatch is ignored.



Although lbColor controls the foreground color of a hatch brush, the SetBkMode and SetBkColor functions control the background color.

 = = = = = = = = = = = = = = = = = = = = = = = =  = = = = = = = = =  = = = = = =

it may have been easier to use the


function . . . .

API is not so easy to learn at first, it takes time to figure it out, and all the Code in the API Help is C language Code, not so good for Pascal developers

 - - - - - - - - -  - - - -
I hope this was a help, ask questions if not enough
LVL 33

Expert Comment

ID: 12242330
oh and in newer systems (Win 98 and after) the -

The bitmap cannot be a DIB section bitmap

is no longer true I think

Author Comment

ID: 12244893
Thanks very much Slick,

Yeah, I've heard that the API stuff is a bit harder to learn.
But you've given me a good starting point here.
I'm on a big learning curve at the moment, it's very challenging :)

LVL 33

Expert Comment

ID: 12245356
there is a web site that has some lessons in doing API programming in Delphi pascal at - -

and I think there is some info about brushes at that site on this page -

but this may not be what you want to do now
LVL 33

Expert Comment

ID: 12245374
Oh yea, you had a question before about this at -

so you might should go to Community Support and ask for a refund of points -

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now