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

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds