直接在设备上下文DC中作图;
void CGdiBitmapView::OnPaintBitmap()
{
// TODO: Add your command handler code here
//取得DC设备
CDC *pDC = GetDC();
//保存DC状态
int nOldDC = pDC->SaveDC();
//获取客户区大小
RECT rect;
GetClientRect(&rect);
//创建与DC兼容的Bitmap
m_pViewBitmap->CreateCompatibleBitmap(pDC, rect.right, rect.bottom);
//选择Bitmap到DC中
pDC->SelectObject(m_pViewBitmap);
//背景填充为紫红色
pDC->FillSolidRect(&rect, 333) ;
//画图
POINT startPoint;
startPoint.x = 0;
startPoint.y = 0;
POINT endPoint;
endPoint.x = rect.right;
endPoint.y = rect.bottom;
pDC->MoveTo(startPoint);
pDC->LineTo(endPoint);
//释放DC资源
pDC->RestoreDC(nOldDC);
ReleaseDC(pDC);
}
在内存DC中作完图,拷贝到DC上(也就是双缓存机制);
void CGdiBitmapView::OnPaintBitmap()
{
// TODO: Add your command handler code here
//取得DC设备
CDC *pDC = GetDC();
//保存DC状态
int nOldDC = pDC->SaveDC();
//创建内存DC
CDC memDC;
memDC.CreateCompatibleDC(pDC);
int nOldMemDC = memDC.SaveDC();
//取得客户区大小
RECT rect;
GetClientRect(&rect);
//创建兼容Bitmap
m_pViewBitmap->CreateCompatibleBitmap(pDC, rect.right, rect.bottom);
//选择Bitmap到内存DC,填充紫红色背景
memDC.SelectObject(m_pViewBitmap);
memDC.FillSolidRect(&rect, 333) ;
//画图
POINT startPoint;
startPoint.x = 0;
startPoint.y = 0;
POINT endPoint;
endPoint.x = rect.right;
endPoint.y = rect.bottom;
memDC.MoveTo(startPoint);
memDC.LineTo(endPoint);
//拷贝内存DC到DC上
pDC->BitBlt(0, 0, rect.right,rect.bottom, &memDC, 0, 0, SRCCOPY);
//释放内存DC资源
memDC.RestoreDC(nOldMemDC);
memDC.DeleteDC();
//释放DC资源
pDC->RestoreDC(nOldDC);
ReleaseDC(pDC);
}