VC中brush的几种用法
2009-05-31 23:06
HBRUSH hbr; 第一种: hbr= CreateSolidBrush(RGB(255,0,0)); //创建单色的画刷 百万英文第二种: hbr= (HBRUSH)GetStockObject(BLACK_BRUSH); //只能取特定颜色的画刷,如BLACK_BRUSH,GRAY_BRUSH刷 第三种: hbr= CreatePatternBrush(HBITMAP hbmp); //得到位图画刷 第四种: hbr = CreateHatchBrush(int fnStyle, //创建一种带阴影的画刷 COLORREF clrref ) 第五种: hbr= CreateBrushIndirect(LOGBRUSH); //通过LOGBRUSH结构体来取画刷 typedef struct tagLOGBRUSH { UINT lbStyle; //画刷类型 COLORREF lbColor; //颜色 LONG lbHatch; //阴影 } LOGBRUSH, *PLOGBRUSH; 第六种: hbr= HBRUSH CreateDIBPatternBrush( //通过与设备无关位图创建一个画刷 HGLOBAL hglbDIBPacked, // handle to DIB UINT fuColorSpec // color table data ); |
|
一个数的最大因数例如:
CBrush brush;
brush.CreateHatchBrush(HS_CROSS,RGB(255,0,0));
pDC->SelectObject(brush);
pDC->Rectangle(400,140,500,160);//水平和垂直交叉的阴影线
得到:
倘若有多个,则:
如:
足球魅力
程序为:
CBrush brush;//创建画刷
brush.CreateSolidBrush(RGB(255,0,0)); // 创建填充色画刷
CBrush *oldbrush=pDC->SelectObject(&brush);生活型态>组练
pDC->Rectangle (100,40,200,60);//红
pDC->SelectObject(oldbrush);// 恢复原来画刷
brush.Detach();//将画刷对象与其构造的内容分类里,以便能再次构造画刷
brush.CreateSolidBrush(RGB(0,255,0));//重新创建画刷
oldbrush=pDC->SelectObject(&brush);
pDC->Rectangle (100,90,200,110);//绿
pDC->SelectObject(oldbrush);// 恢复原来画刷
brush.Detach();//将画刷对象与其构造的内容分类里,以便能再次构造画刷胃部糜烂
brush.CreateSolidBrush(RGB(0,0,255));
oldbrush=pDC->SelectObject(&brush);
pDC->Rectangle(100,140,200,160);//蓝
pDC->SelectObject(oldbrush);// 恢复原来画刷
例1:编写一个单文档应用程序Li3_7,绘制不同颜色、不同阴影形式的填充矩形。
(1) 使用MFC AppWizard 创建一个单文档应用程序Li3_7。
(2) 选择项目工作区的ClassView,展开CLi3_7View 类,打开其成员函数OnDraw( )。
(3) 在函数OnDraw( )中添加代码。
如下图的效果:
程序:
void CLi3_7View::OnDraw(CDC* pDC)
{
CLi3_7Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->SetTextAlign (TA_CENTER);
pDC->TextOut (150,10,"用实心画刷绘图");
pDC->SetTextAlign (TA_CENTER);
pDC->TextOut (450,10,"用阴影画刷绘图");
pDC->SetTextAlign (TA_RIGHT);
pDC->TextOut (70,40,"红");
pDC->SetTextAlign (TA_LEFT);
pDC->TextOut (220,40,"45度从右到左阴影线");
pDC->SetTextAlign (TA_RIGHT);
pDC->TextOut (70,90,"绿");
pDC->SetTextAlign (TA_LEFT);
pDC->TextOut (220,90,"45度从左到右阴影线");
pDC->SetTextAlign (TA_RIGHT);
pDC->TextOut (70,140,"蓝");
pDC->SetTextAlign (TA_LEFT);
pDC->TextOut (220,140,"水平和垂直交叉阴影线");
CBrush brush;
brush.CreateSolidBrush(RGB(255,0,0));
CBrush *oldbrush=pDC->SelectObject(&brush);
pDC->Rectangle (100,40,200,60);//红
pDC->SelectObject(oldbrush);// 恢复原来画刷
brush.Detach();//将画刷对象与其构造的内容分类里,以便能再次构造画刷
brush.CreateSolidBrush(RGB(0,255,0));//重新创建画刷
oldbrush=pDC->SelectObject(&brush);//保存原来的画刷
pDC->Rectangle (100,90,200,110);//绿
pDC->SelectObject(oldbrush);// 恢复原来画刷
brush.Detach();//将画刷对象与其构造的内容分类里,以便能再次构造画刷
brush.CreateSolidBrush(RGB(0,0,255));//重新创建画刷
oldbrush=pDC->SelectObject(&brush);//保存原来的画刷
pDC->Rectangle(100,140,200,160);//蓝
pDC->SelectObject(oldbrush);// 恢复原来画刷
brush.Detach();//将画刷对象与其构造的内容分类里,以便能再次构造画刷
brush.CreateHatchBrush(HS_BDIAGONAL,RGB(0,0,0));//重新创建画刷全球空气质量
oldbrush=pDC->SelectObject(&brush);//保存原来的画刷
pDC->Rectangle (400,40,500,60);//从左下角到右上角的45度阴影线
一只小蜜蜂 pDC->SelectObject(oldbrush);// 恢复原来画刷
brush.Detach();//将画刷对象与其构造的内容分类里,以便能再次构造画刷
brush.CreateHatchBrush(HS_FDIAGONAL,RGB(0,0,0));//重新创建画刷
oldbrush=pDC->SelectObject(&brush);//保存原来的画刷
pDC->Rectangle (400,90,500,110);//从左上角到右下角的45度阴影线
pDC->SelectObject(oldbrush);// 恢复原来画刷
brush.Detach();//将画刷对象与其构造的内容分类里,以便能再次构造画刷
brush.CreateHatchBrush(HS_CROSS,RGB(0,0,0));//重新创建画刷
oldbrush=pDC->SelectObject(&brush);//保存原来的画刷
pDC->Rectangle(400,140,500,160);//水平和垂直交叉的阴影线
pDC->SelectObject(oldbrush);// 恢复原来画刷
}
在OnDraw( )函数中,将函数SetTextColor( )和SetBkColor( )添加到TextOut( )函数调用之前,就可以将颜色加到你的应用程序中。