Creating Brushes in the Win32 API

How to Use CreateBrushIndirect, CreateSolidBrush, Pattern and Hatch

© Guy Lecky-Thompson

Nov 21, 2008
Painting with Brushes, Sophie
Using the Win32 API to fill areas by creating custom GDI objects called brushes, including solid brushes, pattern brushes and hatch brushes.

To draw a filled object (such as a Rectangle), the application must select a brush into the device context, using the SelectObject function. In order to create a brush, there are several functions available in the Win32 GDI API:

  • CreateSolidBrush - create a brush of solid color
  • CreateHatchBrush - create a brush using a predefined pattern
  • CreatePatternBrush - create a brush based on a bitmap
  • CreateBrushIndirect - create a brush based on a LOGBRUSH structure

Once the brush is created, it can then be selected into the device context in the same way as any other GDI object. In addition, the DeleteObject function can be used when the brush is no longer needed, provided it has been selected out of the device context first.

The GDI Function CreateSolidBrush

This function takes a COLORREF value, as created with the RGB function, and returns a single color brush. An example call might be:

HBRUSH hBrush = CreateSolidBrush(RGB(255,0,0)); // red brush

In order to introduce some patterns with the standard brush, the CreateHatchBrush function can be used.

The GDI Function CreateHatchBrush

This function takes a constant that identifies what kind of hatching (in the selected pen color) to use, and a COLORREF that is used for the actual foreground hatching color. An example call might be:

HBRUSH hBrush = CreateHatchBrush(HS_CROSS, RGB(255,0,0)); // red brush, horizontal and vertical

The other hatching values are as follows:

  • HS_HORIZONTAL, HS_VERTICAL, HS_CROSS : various stripes and crosses
  • HS_DIAGCROSS : diagonal cross
  • HS_BDIAGONAL, HS_FDIAGONAL : diagonal stripes

To have more control over the hatching used, the programmer can create a pattern brush based on a monochrome bitmap.

The GDI Function CreatePatternBrush

The CreatePatternBrush takes the handle to a bitmap, which can be either a color or monochrome bitmap. If the bitmap is larger than the area to be filled, it will be clipped. If it is smaller, it will be repeated. An example call to this function might be:

HBRUSH hBrush = CreateHatchBrush(hBitmap); // bitmap brush

A monochrome bitmap will result in the 1 bit pixels being represented by the text background color, and 0 bit pixels in the text foreground color. To set these, the SetTextColor and SetBkColor functions are used. Each of these take the device context handle (HDC) and a color (COLORREF) as arguments.

The GDI Function CreateBrushIndirect

The final function for creating brushes, CreateBrushIndirect, takes a LOGBRUSH function, which can be populated by sampling an existing brush, or by specifying all the parameters manually. To populate the structure from an existing brush, code such as the following can be used:

GetObject(hBrush, sizeof(LOGBRUSH), &lbBrush);

The LOGBRUSH structure has three members:

  • lbStyle - the style of brush, using a BS_ identifier
  • lbColor - a COLORREF used to determine the color for brushes except BS_HOLLOW or BS_PATTERN brushes
  • lbHatch - a LONG value which is either a hatching identifier, or packed bitmap data

Some of the more common lbStyle values are:

  • BS_PATTERN - as in the CreatePatternBrush function
  • BS_HATCHED - as in the CreateHatchBrush function
  • BS_SOLID - a solid brush

The lbHatch values are the same as above, for the two types of pattern brush.

Using and Deleting the Brush

The brush is selected into the device context using the SelectObject function, in th usual mmanner. It can be deleted using the DeleteObject function, too, however note that deleting the brush does not delete a bitmap associated with it. So, these must be deleted separately, but it does mean that the same brush can be used for multiple bitmaps or patterns.


The copyright of the article Creating Brushes in the Win32 API in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Creating Brushes in the Win32 API in print or online must be granted by the author in writing.


Painting with Brushes, Sophie
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo