Creating Pattern Brushes

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: VC6

Often, in developing PageMaker- and CAD-related applications, one needs the functionality of filling interiors of rectangles, ellipses, polygons, and paths such as objects. This article introduces how to fill rectangles. This article also includes how to create our own brush style.

To fill a rectangle, we use bitmaps and brushes. The application created here uses MFC AppWizard(EXE) With SDI.

The application has an Onwards Brush Menu. When you select any menu item, the interior of the rectangle is filled with a specified pattern brush. The main code of this application is as follows:

CBitmap bm1;
CDC *pDC=GetDC();
CBrush brush,*pOldBrush;
WORD HatchBits1[8]= { 0xff, 0xff, 0x81, 0xbd,0xbd,0xbd, 0x81,
                      0xff };
bm1.CreateBitmap(8,8,1,1, HatchBits1);

Without telling how HatchBits[8] data are initialized, this article is totally irrelevant. A computer stores bytes in memory in two ways: Big Endian and Little Endian. Little Endian means that the little end of a multibyte value is stored first. For example, the value 0x1234 is stored in memory as 0x34 0x12. Intel CPUs are Little Endian. Win32 supports only Little Endian processors. The Windows BMP format, since it was developed on a “Little Endian” architecture, insists on the “Little Endian” format. The BMP format stores image data from bottom to top. You draw a bitmap in MSPaint at an 8 by 8 size and save it has a monochrome bitmap. The Monochrome means each bit is on (1) or off (0). The offbit(0) bit means white; the onbit(1) bit means black. Open any hexeditor; from that, open a bitmap file. You see the bitmap data bits starting from the last because the BMP format stores image data from the bottom to the top.

You also can use the CreateOwnbrush dialog in this application to create pattern brushes. The CreatePatternBrush function creates a logical brush with the specified bitmap pattern. Select that brush into deviceContext for drawing.


The dialog box contains three edit fields for the RGB value and three buttons. The purpose of this dialog box is to change the color of pattern brush with the specified color. The View window acts as a parent to this dialog box. In the constructor of this dialog box, pass the CCustomBrushView object pointer.

The fuctionality of buttons:

Button Fuctionality
OK Changes the color of brush of the rectangle in view with the specified color in RGB fields and immediately closes the dialog box.
Cancel The brush color is unchanged; that is, black.
Apply Changes the color of brush of the rectangle in view with specified color in RGB fields and dialog box does not close.

Create Your Own Pattern Brush Dialog

When you select MenuItem CreateOwnBrush under the Brush menu, the dialog box contains a grid that represents the bitmap the system uses as a brush. A user can use this grid to create a pattern-brush bitmap and then view the custom pattern by clicking the Test button. The bitmap data is displayed in the edit control. The whole functionality is written in the COwnBrushDialog class.

The dialog box procedure for the Custom Brush dialog box processes four messages, as described in the following table.

Message Message Action
WM_INITDIALOG Retrieves dimensions for the grid window, computes the dimensions of a single cell in the grid window control, and initializes an array of grid-cell coordinates.
WM_PAINT Draws the grid pattern in the grid window control.
WM_LBUTTONDOWN Determines whether the cursor is within the grid window control. If so, the dialog box procedure inverts the appropriate grid cell and records the state of that cell in an array of bits that is used to create the bitmap for the custom brush.
WM_COMMAND Processes input for the three push button controls.


Download demo project – 87 Kb

Download source – 65 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read