CBrush::CBrush
Constructs a CBrush object.
CBrush( );
CBrush(
COLORREF crColor
);
CBrush(
int nIndex,
COLORREF crColor
);
explicit CBrush(
CBitmap* pBitmap
);
Parameters
crColor
Specifies the foreground color of the brush as an RGB color. If the brush is hatched, this parameter specifies the color of the hatching.nIndex
Specifies the hatch style of the brush. It can be any one of the following values:HS_BDIAGONAL Downward hatch (left to right) at 45 degrees
HS_CROSS Horizontal and vertical crosshatch
HS_DIAGCROSS Crosshatch at 45 degrees
HS_FDIAGONAL Upward hatch (left to right) at 45 degrees
HS_HORIZONTAL Horizontal hatch
HS_VERTICAL Vertical hatch
pBitmap
Points to a CBitmap object that specifies a bitmap with which the brush paints.
Remarks
CBrush has four overloaded constructors.The constructor with no arguments constructs an uninitialized CBrush object that must be initialized before it can be used.
If you use the constructor with no arguments, you must initialize the resulting CBrush object with CreateSolidBrush, CreateHatchBrush, CreateBrushIndirect, CreatePatternBrush, or CreateDIBPatternBrush. If you use one of the constructors that takes arguments, then no further initialization is necessary. The constructors with arguments can throw an exception if errors are encountered, while the constructor with no arguments will always succeed.
The constructor with a single COLORREF parameter constructs a solid brush with the specified color. The color specifies an RGB value and can be constructed with the RGB macro in WINDOWS.H.
The constructor with two parameters constructs a hatch brush. The nIndex parameter specifies the index of a hatched pattern. The crColor parameter specifies the color.
The constructor with a CBitmap parameter constructs a patterned brush. The parameter identifies a bitmap. The bitmap is assumed to have been created by using CBitmap::CreateBitmap, CBitmap::CreateBitmapIndirect, CBitmap::LoadBitmap, or CBitmap::CreateCompatibleBitmap. The minimum size for a bitmap to be used in a fill pattern is 8 pixels by 8 pixels.
Example
// CBrush::CBrush.
CBrush brush1; // Must initialize!
brush1.CreateSolidBrush(RGB(0, 0, 255)); // Blue brush.
CBrush* pTempBrush = NULL;
CBrush OrigBrush;
CRect rc;
GetClientRect(&rc);
ScreenToClient(&rc);
pTempBrush = (CBrush*)pDC->SelectObject(&brush1);
// Save original brush.
OrigBrush.FromHandle((HBRUSH)pTempBrush);
// Paint upper left corner with blue brush.
pDC->Rectangle(0, 0, rc.Width() / 2, rc.Height() / 2);
// These constructors throw resource exceptions.
try
{
// CBrush::CBrush(COLORREF crColor)
CBrush brush2(RGB(255, 0, 0)); // Solid red brush.
// CBrush::CBrush(int nIndex, COLORREF crColor)
// Hatched green brush.
CBrush brush3(HS_DIAGCROSS, RGB(0, 255, 0));
// CBrush::CBrush(CBitmap* pBitmap)
CBitmap bmp;
// Load a resource bitmap.
bmp.LoadBitmap(IDB_BRUSH);
CBrush brush4(&bmp);
pTempBrush = (CBrush*)pDC->SelectObject(&brush2);
// Paint upper right corner with red brush.
pDC->Rectangle(rc.Width() / 2, 0, rc.Width(),
rc.Height() / 2);
pTempBrush = (CBrush*)pDC->SelectObject(&brush3);
// Paint lower left corner with green hatched brush.
pDC->Rectangle(0, rc.Height() / 2, rc.Width() / 2,
rc.Height());
pTempBrush = (CBrush*)pDC->SelectObject(&brush4);
// Paint lower right corner with resource brush.
pDC->Rectangle(rc.Width() / 2, rc.Height() / 2,
rc.Width(), rc.Height());
}
catch(CResourceException* e)
{
e->ReportError();
e->Delete();
}
// Reselect original brush into device context.
pDC->SelectObject(&OrigBrush);
Requirements
Header: afxwin.h