如何在Bitmap中画图?(MFC)


直接在设备上下文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);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM