[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

Anyone know how to create custom brushes for TShape?

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
nathc
Asked:
nathc
1 Solution
 
ginsonicCommented:
 Canvas.Font.Color := ForegroundColor;
  Canvas.Brush.Bitmap := Bmp;
  SetBKColor(Canvas.Handle, ColorToRGB(BackgroundColor));
  Canvas.FillRect(ClientRect);
0
 
Slick812Commented:
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
 
Slick812Commented:
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
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?
-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
 
pritaeasSoftware EngineerCommented:
You can look up the API function at msdn.microsoft.com
0
 
Slick812Commented:
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
 
Slick812Commented:
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
 
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 :)

nathc
0
 
Slick812Commented:
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
 
Slick812Commented:
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

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now