1.加载背景图片
在OnPaint()中加入如下代码:
CDC *pDC = GetDC();
CBitmap Bitmap;
Bitmap.LoadBitmap(IDB_BITMAP);//将位图装入内存
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);//创建内存设备环境
CBitmap *OldBitmap=MemDC.SelectObject(&Bitmap);
//创建BITMAP 结构变量
BITMAP bm;
//获取位图信息
Bitmap.GetBitmap(&bm);
//显示位图
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&MemDC,0,0,SRCCOPY);
//恢复设备环境
OldBitmap->DeleteObject();
ReleaseDC(pDC);
2.在对话框中画图
加入如下代码:
CDC *pDC = GetDC();
CBrush brushColor(RGB(255,255,0));//填充的颜色
CPen penColor;
penColor.CreatePen(PS_SOLID,1,RGB(0,64,128));//边框颜色
pDC->SelectObject(penColor);
pDC->SelectObject(brushColor);
pDC->Rectangle(rect);
brushColor.DeleteObject();
penColor.DeleteObject();
ReleaseDC(pDC);
3 .改变背景颜色
在OnPaint()中加入如下代码:
a. 方法1
CPaintDC dc(this);
CRect rect;
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(255,0,0));
b. 方法2
CDC *pDC = this->GetDC();
CBrush brushColor(RGB(255,255,0));//填充的颜色
CPen penColor;
penColor.CreatePen(PS_SOLID,1,RGB(0,64,128));//边框颜色
pDC->SelectObject(penColor);
pDC->SelectObject(brushColor);
CRect rect;
GetClientRect(rect);
pDC->Rectangle(rect);
brushColor.DeleteObject();
penColor.DeleteObject();
ReleaseDC(pDC);