MFC中GDI之CBrush(画刷)

CBrush 画刷主要是修饰一个闭合图形内部的填充内容,包括填充色、填充影线和填充位图等。

CBrush(COLORREF crColor) 在构造函数中根据填充色创建画刷
CBrush(int nIdex, COLORREF crColor) 在构造函数中根据颜色和影线创建画刷
CBrush(CBitmap* pBitmap) 在构造函数中根据平铺位图创建画刷
BOOL CreateSolidBrush(COLORREF crColor) 根据填充色创建画刷
BOOL CreateHatchBrush(int nIndex, COLORREF crColor) 根据颜色和影线创建画刷
BOOL CreatePatternBrush(CBitmap* pBitmap) 根据平铺位图创建画刷
BOOL CreateBrushIndirect(const LOGBRUSH* lpBrush) 根据LOGBRUSH结构体创建画刷
static CBrush* FromHandle(HBRUSH hBrush) 将HBRUSH句柄转换为CBrush对象
operator HBRUSH() const 从CBrush对象获取HBRUSH句柄
int GetLogBrush(LOGBRUSH* pLogBrush) 获取画刷的颜色和影线等属性信息

wingdi.ini中的一些定义:

/* Brush Styles */
#define BS_SOLID            0
#define BS_NULL             1
#define BS_HOLLOW           BS_NULL
#define BS_HATCHED          2
#define BS_PATTERN          3
#define BS_INDEXED          4
#define BS_DIBPATTERN       5
#define BS_DIBPATTERNPT     6
#define BS_PATTERN8X8       7
#define BS_DIBPATTERN8X8    8
#define BS_MONOPATTERN      9

/* Hatch Styles */
#define HS_HORIZONTAL       0       /* ----- */
#define HS_VERTICAL         1       /* ||||| */
#define HS_FDIAGONAL        2       /* \\\\\ */
#define HS_BDIAGONAL        3       /* ///// */
#define HS_CROSS            4       /* +++++ */
#define HS_DIAGCROSS        5       /* xxxxx */


/* Logical Brush (or Pattern) */
typedef struct tagLOGBRUSH
  {
    UINT        lbStyle;
    COLORREF    lbColor;
    LONG        lbHatch;
  } LOGBRUSH, *PLOGBRUSH;

 例子:

void CMFC7_5BrushDlg::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    // 创建纯色画刷
    CBrush b1(RGB(0,255,255));//青色
/*    CBrush b1;
    b1.CreateSolidBrush(RGB(0,255,255)); */ //这两句等价于上面的一句
    dc.SelectObject(&b1);
    CRect rect(10,10,110,160);
    dc.RoundRect(&rect,CPoint(20,20));
    // 创建格栅画刷
    CBrush b2(HS_CROSS,RGB(0,0,255));
/*    CBrush b2;
    b2.CreateHatchBrush(HS_FDIAGONAL,RGB(255,0,0)); */
    dc.SelectObject(&b2);
    CRect rect2(120,10,220,160);
    dc.RoundRect(&rect2,CPoint(20,20));
    // 创建位图画刷
    CBitmap bmp;
    bmp.LoadBitmap(IDB_BITMAP1);
    CBrush b3(&bmp);
/*    CBrush b3;
    b3.CreatePatternBrush(&bmp); */
    dc.SelectObject(&b3);
    CRect rect3(230,10,340,160);
    dc.RoundRect(&rect3,CPoint(20,20));
    // 创建一个空画刷
    CBrush b4;
    LOGBRUSH logBrush = {BS_NULL};
    b4.CreateBrushIndirect(&logBrush);
    dc.SelectObject(&b4);
    CPoint ps[] ={CPoint(170, 10),CPoint(120, 150),CPoint(220, 150)};
    dc.Polygon(ps, sizeof(ps)/sizeof(ps[0]));

    // 还有一种更简单的方式(一句代码包括了创建和选择)
    // CDC::SelectStockObject 选择储备的对象
/*
    virtual CGdiObject* SelectStockObject ( int nIndex ); 
        Parameters :    nIndex 
    The kind of stock object desired. It can be one of the following values: 
        BLACK_BRUSH                Black brush. 
        DKGRAY_BRUSH            Dark gray brush. 
        GRAY_BRUSH                Gray brush. 
        HOLLOW_BRUSH            Hollow brush.  == NULL_BRUSH
        LTGRAY_BRUSH            Light gray brush. 
        NULL_BRUSH                Null brush. 
        WHITE_BRUSH                White brush. 
        BLACK_PEN                Black pen. 
        NULL_PEN                Null pen. 
        WHITE_PEN                White pen. 
        DEVICE_DEFAULT_FONT        Device-dependent font. 
        SYSTEM_FONT                The system font  
*/
    dc.SelectStockObject(GRAY_BRUSH);
    dc.Rectangle(20,200,80,250);
}

 结果:

----------------------------------

注:参考吕鑫MFC的7.5章节

 

 

// 创建一个空画刷的方法:

1.

CBrush b4;
LOGBRUSH logBrush = {BS_NULL};
b4.CreateBrushIndirect(&logBrush);
dc.SelectObject(&b4);//将空画刷选入设备描述表

2.

CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush = dc.SelectObject(pBrush);

3.

dc.SelectStockObject(NULL_BRUSH);

 

posted @ 2019-11-27 21:59  htj10  阅读(1302)  评论(0编辑  收藏  举报
TOP