Bitmap Topics: Using as a Patterned Brush

 

Overview

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,
		        0x88, 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:

Bitmap 1

To create a brush based on a bitmap, you can use the following constructor:

CBrush(CBitmap* pBitmap);

If you had declared a CBrush variable using the default constructor, you can call the CBrush::CreatePatternBrush() member function to initialize it. Its syntax is:

BOOL CreatePatternBrush(CBitmap* pBitmap);

Here is an example:

void CExoView::OnDraw(CDC* pDC)
{
	CExoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CBrush brPattern;
	CBitmap Bmp;
	CBrush *pBrush;

	Bmp.LoadBitmap(IDB_BITMAP1);

	brPattern.CreatePatternBrush(&Bmp);

	pBrush = pDC->SelectObject(&brPattern);
	pDC->Rectangle(46, 46, 386, 386);

	pDC->SelectObject(pBrush);
}
Bitmap 2

 

 

Home Copyright © 2003-2006 FunctionX, Inc.