Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 8 GDI Orientation and Transformations


color (or picture) that the brush holds would be used to fill the whole area until the brush
finds a limit set by some rule.

A brush can be characterized by its color (if used), its pattern used to fill the area, or a
picture (bitmap) used as the brush.

To create a brush, the MFC provides the CBrush class. Therefore, to start, you can
declare a variable of this type using the default constructor as follows:

CBrush NewBrush;

Because there can be so many variations of brushes, there are different member functions
for the various possible types of brushes you would need. The easiest brush you can
create is made of a color.

7.6.2 Solid Brushes........................................................................................


A brush is referred to as solid if it is made of a color simply used to fill a closed shaped.
To create a solid brush, you can use the following constructor:

CBrush(COLORREF crColor);

The color to provide as the crColor argument follows the rules we reviewed for colors.

To use the newly created brush, you can select it into the device context by calling the
CDC::SelectObject(). Once this is done. Any closed shape you draw (ellipse, rectangle,
polygon) would be filled with the color specified. After using the brush, you can dismiss
it and restore the previous brush. Here is an example:

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

CBrush NewBrush(RGB(250, 25, 5));

CBrush *pBrush = pDC->SelectObject(&NewBrush);
pDC->Rectangle(20, 20, 250, 125);

pDC->SelectObject(pBrush);
}
Free download pdf