CBrush::CBrush

构造 CBrush 对象。

CBrush( );
CBrush(
   COLORREF crColor 
);
CBrush(
   int nIndex,
   COLORREF crColor 
);
explicit CBrush(
   CBitmap* pBitmap 
);

参数

  • crColor
    指定画笔的前景色为RGB颜色。如果画笔将阴影,此参数指定阴影的颜色。

  • nIndex
    指定画笔的阴影样式。它可以是以下任一值:

    • HS_BDIAGONAL 向下阴影(从左到右)在45度

    • HS_CROSS 水平和垂直跨阴影线

    • 在45度的HS_DIAGCROSS 跨阴影线

    • HS_FDIAGONAL 向上阴影(从左到右)在45度

    • HS_HORIZONTAL 级别的阴影

    • HS_VERTICAL 垂直阴影

  • pBitmap
    指向 CBitmap 指定位图画笔绘制的对象。

备注

CBrush 有四个重载的构造函数。无参数的构造函数构造必须初始化的未初始化的 CBrush 对象,才能使用它。

如果使用构造函数没有参数,必须初始化 CreateSolidBrushCreateHatchBrushCreateBrushIndirectCreatePatternBrushCreateDIBPatternBrush的结果 CBrush 对象。如果使用接受参数的构造函数之一,则进一步初始化并不是必需的。带参数的构造函数可以引发异常,如果遇到错误,则,而无参数的构造函数将总是成功。

与单个 COLORREF 参数的构造函数构造与指定的颜色绘制实心画笔。颜色指定RGB值,并可以构造与WINDOWS.H.的 RGB 宏。

带有两个参数的构造函数构造阴影画笔。nIndex 参数指定一个阴影的模式的索引。crColor 参数指定颜色。

CBitmap 参数的构造函数构造一个新位图的画笔。该参数标识位图。使用 CBitmap::CreateBitmapCBitmap::CreateBitmapIndirectCBitmap::LoadBitmapCBitmap::CreateCompatibleBitmap,位图假定已创建。用于填充模式中的位图的最小大小为8 x 8像素。

示例

// 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);

要求

Header: afxwin.h

请参见

参考

CBrush选件类

层次结构图

CBrush::CreateSolidBrush

CBrush::CreateHatchBrush

CBrush::CreateBrushIndirect

CBrush::CreatePatternBrush

CBrush::CreateDIBPatternBrush

CGdiObject::CreateStockObject