DC操作的问题
我在mobile 平台使用建立了一个基于MFC的工程来测试双缓冲,主要目的是将两张图片在内存DC中合并完毕后一次性拷贝到显存,防止闪烁问题存在。现在主要面临如下问题:
1.小图片在合并至大图片的时候无法正常合并(第5步),提示错误为87,查询资料是参数传递不对,但是我自认为参数没啥问题。所以请各位大侠帮忙看下
OnPaint函数实现代码如下:
//1.获取屏幕设备DC
CPaintDC pDeviceDC(this);
//CDC *pDeviceDC = this->GetDC();
HDC hDeviceDC = ::GetDC(this->m_hWnd);
//2.创建兼容DC
HDC hmemDC;
HDC hmenDC1;
HBITMAP hbigBmp,hsmallBmp;
hmemDC = ::CreateCompatibleDC(hDeviceDC/*pDeviceDC.GetSafeHdc()*/);
hmenDC1 = ::CreateCompatibleDC(hDeviceDC/*pDeviceDC.GetSafeHdc()*/);
//3.加载位图
hbigBmp = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BIG));
hsmallBmp = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(/*IDB_DISCONN*/IDB_SMALL));
//4.大位图选入兼容DC
BITMAP bmpInfo1,bmpInfo2;
HBITMAP holdBmp =(HBITMAP)::SelectObject(hmemDC,hbigBmp);
HBITMAP holdBmp2 = (HBITMAP)::SelectObject(hmenDC1,hsmallBmp);
CBitmap * pbmp = CBitmap::FromHandle(hsmallBmp);
pbmp->GetBitmap(&bmpInfo2);
////5,两图合并
bRet = ::BitBlt(hmemDC,0,0,/*bmpInfo2.bmWidth*/240,254/*bmpInfo2.bmHeight*/,hmenDC1,0,0,SRCCOPY);
if (!bRet)
{
DWORD dwErro = GetLastError();
printf("memDC.BitBlt Error code = %d,width=%d,height=%d\r\n",dwErro,bmpInfo2.bmWidth,bmpInfo2.bmHeight);
}
//6,写字
RECT rcText;
rcText.left = 40;
rcText.top = 40;
rcText.right = 200;
rcText.bottom = 80;
::SetBkMode(hmemDC,TRANSPARENT);
CString strText = _T("hello world");
::DrawText(hmemDC,strText,strText.GetLength(),&rcText,DT_LEFT);
pbmp = CBitmap::FromHandle(hbigBmp);
pbmp->GetBitmap(&bmpInfo1);
bRet = ::BitBlt(pDeviceDC.GetSafeHdc(),0,0,bmpInfo1.bmWidth,bmpInfo1.bmHeight,hmemDC,0,0,SRCCOPY);
if (!bRet)
{
DWORD dwErro = GetLastError();
printf("pDeviceDC->BitBlt Error code = %d,width=%d,height=%d\r\n",dwErro,bmpInfo1.bmWidth,bmpInfo1.bmHeight);
}
::SelectObject(hmemDC,holdBmp);
::SelectObject(hmenDC1,holdBmp2);
::DeleteObject(hbigBmp);
::DeleteObject(hsmallBmp);
::DeleteDC(hmemDC);
::DeleteDC(hmenDC1);
::ReleaseDC(this->m_hWnd,hDeviceDC);
[解决办法]
hmemDC = ::CreateCompatibleDC(hDeviceDC/*pDeviceDC.GetSafeHdc()*/);
hmenDC1 = ::CreateCompatibleDC(hDeviceDC/*pDeviceDC.GetSafeHdc()*/);
//这个的问题,按照层次关系,先把hmemDC 和hmenDC1 兼容,然后pDeviceDC和hmemDC ,不是两个都和pDeviceDC兼容
[解决办法]
你将第二副图,生成一个刷子然后在加载上去试试。
下面我写的一段参考代码:
CDC memDC;
memDC.CreateCompatibleDC(pDC);
BITMAP bmp;
bitmap_desk.GetBitmap(&bmp);
int bmpWidth = bmp.bmWidth;
int bmpHeight = bmp.bmHeight;
CBitmap memBitmap;
memBitmap.CreateCompatibleBitmap(pDC,bmpWidth,bmpHeight);
CBitmap *pOldBitmap = memDC.SelectObject(&memBitmap);
CRect rect(0,0,bmpWidth,bmpHeight);
CBrush mBrush;
mBrush.CreatePatternBrush(&bitmap_desk);
CBrush * pOldBrush = memDC.SelectObject(&mBrush);
memDC.FillRect(rect,&mBrush);
UINT uSpeedNum;
CSystemPara m_SystamPara;
if (m_SystamPara.bSpeedStatus==TRUE)
uSpeedNum=5;
else if(m_SystamPara.bSpeedStatus==FALSE)
uSpeedNum=15;
if(m_iCurState==1)
{
DisplaySpeed1(&memDC);
m_draw.DisplayProductConvertGDI(&memDC);
}
if (m_iCurState==0)
{
m_draw.DisplayTestGDI(&memDC,uSpeedNum);
m_draw.DisplayResultMap(&memDC);
m_draw.DisplayCoordate(&memDC);
}
pDC->BitBlt(0,0,bmpWidth,bmpHeight,&memDC,0,0,SRCCOPY);
memDC.SelectObject(pOldBrush);
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();