Solved

Anyone know how to create custom brushes for TShape?

Posted on 2004-10-05
10
583 Views
Last Modified: 2010-04-05
Hi,

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.

Thanks
Nathc
0
Comment
Question by:nathc
10 Comments
 
LVL 9

Expert Comment

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

Expert Comment

by:Slick812
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;
  begin
  if lim < 0 then Result := 0
    else Result := lim and $FF;
  end;

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

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

brBmp.Canvas.Pen.Color := gdColor;
brBmp.Canvas.MoveTo(0,0);
brBmp.Canvas.LineTo(8,0);
brBmp.Canvas.MoveTo(0,1);
brBmp.Canvas.LineTo(0,8);
BrushLog1.lbStyle := BS_PATTERN;
BrushLog1.lbHatch := brBmp.Handle;
Result := CreateBrushIndirect(BrushLog1);
FreeAndNil(brBmp);
end;


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

ask questions if you need more information
0
 
LVL 33

Expert Comment

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

brBmp.Canvas.MoveTo(0,0);
brBmp.Canvas.LineTo(7,0);
brBmp.Canvas.MoveTo(0,1);
brBmp.Canvas.LineTo(0,7);
0
 

Author Comment

by:nathc
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?
-GetBValue(BkColor)
-CreateBrushIndirect

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).

Cheers
Nathc

0
 
LVL 6

Expert Comment

by:pritaeas
ID: 12238223
You can look up the API function at msdn.microsoft.com
0
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

by:
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

http://msdn.microsoft.com/library/

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;
} LOGBRUSH;
 

Members

lbStyle

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_DIBPATTERN8X8      Same as BS_DIBPATTERN.
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_PATTERN8X8      Same as BS_PATTERN.
BS_SOLID      Solid brush.
 

lbColor

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.
 

lbHatch

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.

 

Remarks

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

CreatePatternBrush(brBmp.Handle);

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
0
 
LVL 33

Expert Comment

by:Slick812
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
0
 

Author Comment

by:nathc
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 :)

nathc
0
 
LVL 33

Expert Comment

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

http://www.angelfire.com/hi5/delphizeus/index.html

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

http://www.angelfire.com/hi5/delphizeus/brushpen.html

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

Expert Comment

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

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21153776.html

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

http://www.experts-exchange.com/Community_Support/
0

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 (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. 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