Creating Pattern Brushes

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.

Click here for a larger image.

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


  • Bad while printing fill pattern

    Posted by drinker on 02/15/2006 07:57pm

    I used the method specified in the article. It is OK for display in the display device. But while printing, for some printer(e.g. Acrobat PDF 7.0, some HP printers), the pattern is printed as solid. (The fill pattern is printed so dense that it looks like solid). It there any solution for the print problem?

  • custom brush

    Posted by Legacy on 08/05/2003 12:00am

    Originally posted by: mahi

    very good.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date