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);
brush.CreatePatternBrush(&bm1);
*pOldBrush=pDC->SelectObject(&brush)
pDC->Rectangle(100,100,200,200)
pDC->SelectObject(pOldBrush)

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.

ColorDialog

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.

Downloads

Download demo project - 87 Kb
Download source - 65 Kb


Comments

  • 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?

    Reply
  • custom brush

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

    Originally posted by: mahi

    very good.

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds