Visual C++ and MFC Fundamentals Chapter 8 GDI Orientation and Transformations
7.6.4 Patterned Brushes................................................................................
A pattern brush is one that uses a bitmap or (small) picture to fill out an area. To create
DDB bitmap, you can first create an array of WORD values. Then call the
CBitmap::CreateBitmap() method to initialize it. As this makes the bitmap ready, call
the CBrush::CreatePatternBrush() method to initialize the brush. The syntax of this
member function is:
BOOL CreatePatternBrush(CBitmap* pBitmap);
Once the brush has been defined, you can select in into a device context and use it as you
see fit. For example, you can use it to fill a shape. Here is an example:
void CCView4View::OnDraw(CDC* pDC)
{
CCView4Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CBitmap Bmp;
CBrush brBits;
WORD wBits[] = { 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88,
0x22, 0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00,
0x44, 0x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22,
0 x88, 0x00, 0x22, 0x44, 0x88, 0x00, 0x22, 0x44 };
Bmp.CreateBitmap(32, 32, 1, 1, wBits);
brBits.CreatePatternBrush(&Bmp);
CBrush* pOldBrush = (CBrush*)pDC->SelectObject(&brBits);
pDC->Rectangle(20, 20, 400, 400);
pDC->SelectObject(&Bmp);
}
Another technique you can use to create a pattern brush consists of using a bitmap
resource. Before creating a pattern, you must first have a picture, which can be done by
creating a bitmap. For example, imagine you create the following bitmap: