// 逻辑1:先从资源中读取背景资源,然后将绘图对象与DC绑定,通过绘图对象绘出背景
// 逻辑2:先从资源中读取背景资源,新建一个MEMDC,将绘图对象与MEMDC绑定,并且
// 通过绘图对象在内存中绘制图形, 最后将MEMDC中的数据复制给绘图DC绘图
Gdiplus::Image* m_pPngBg;
1 BOOL G_ImageFromIDResource( UINT nID, LPCTSTR sTR,Gdiplus::Image *&pImg ) 2 { 3 HINSTANCE hInst = AfxGetResourceHandle(); 4 HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type 5 if (!hRsrc) 6 return FALSE; 7 // load resource into memory 8 DWORD len = SizeofResource(hInst, hRsrc); 9 BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc); 10 if (!lpRsrc) 11 return FALSE; 12 // Allocate global memory on which to create stream 13 HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len); 14 BYTE* pmem = (BYTE*)GlobalLock(m_hMem); 15 memcpy(pmem,lpRsrc,len); 16 GlobalUnlock(m_hMem); 17 IStream* pstm; 18 CreateStreamOnHGlobal(m_hMem,FALSE,&pstm); 19 // load from stream 20 pImg=Gdiplus::Image::FromStream(pstm); 21 // free/release stuff 22 pstm->Release(); 23 FreeResource(lpRsrc); 24 GlobalFree(m_hMem); 25 return TRUE; 26 }
方法一:方便
G_ImageFromIDResource(IDB_PNG_MONEYSEL_SELECTED, _T("PNG"), m_pPngBg);
Gdiplus::Graphics graphics(pDC->m_hDC);
graphics.DrawImage(m_pPngBg, 0, 0,m_rcClient.Width(),m_rcClient.Height());
方法二:推荐
CRect rect;
GetWindowRect(&rect);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
int nWidth=m_pPngBg->GetWidth();
int nHeight=m_pPngBg->GetHeight();
CBitmap bitMemMap;
bitMemMap.CreateCompatibleBitmap(pDC,nWidth,nHeight);
memDC.SelectObject(&bitMemMap);
Gdiplus::Graphics graphics(memDC.m_hDC); // Create a GDI+ graphics object
graphics.DrawImage(m_pPngBg, 0, 0,nWidth,nHeight);
pDC->BitBlt(0,0, rect.Width(),rect.Height(),&memDC, 0, 0,SRCCOPY);
bitMemMap.DeleteObject();
memDC.DeleteDC();