|
||||||
Creating Brushes in the Win32 APIHow to Use CreateBrushIndirect, CreateSolidBrush, Pattern and Hatch
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:
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 CreateSolidBrushThis 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 CreateHatchBrushThis 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:
To have more control over the hatching used, the programmer can create a pattern brush based on a monochrome bitmap. The GDI Function CreatePatternBrushThe 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 CreateBrushIndirectThe 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:
Some of the more common lbStyle values are:
The lbHatch values are the same as above, for the two types of pattern brush. Using and Deleting the BrushThe 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.
|
||||||
|
|
||||||
|
|
||||||