Anyone know how to create custom brushes for TShape?


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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 Canvas.Font.Color := ForegroundColor;
  Canvas.Brush.Bitmap := Bmp;
  SetBKColor(Canvas.Handle, ColorToRGB(BackgroundColor));
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
you might also try the look of this variation -

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

nathcAuthor Commented:
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).


pritaeasSoftware EngineerCommented:
You can look up the API function at
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oh and in newer systems (Win 98 and after) the -

The bitmap cannot be a DIB section bitmap

is no longer true I think
nathcAuthor Commented:
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 :)

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
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 -
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.