图形绘制处理逻辑VC
阅读原文时间:2023年07月12日阅读:1

// 逻辑1:先从资源中读取背景资源,然后将绘图对象与DC绑定,通过绘图对象绘出背景
// 逻辑2:先从资源中读取背景资源,新建一个MEMDC,将绘图对象与MEMDC绑定,并且
// 通过绘图对象在内存中绘制图形, 最后将MEMDC中的数据复制给绘图DC绘图

Gdiplus::Image* m_pPngBg;

BOOL G_ImageFromIDResource( UINT nID, LPCTSTR sTR,Gdiplus::Image *&pImg )
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
if (!hRsrc)
return FALSE;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE;
// Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
GlobalUnlock(m_hMem);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
// load from stream
pImg=Gdiplus::Image::FromStream(pstm);
// free/release stuff
pstm->Release();
FreeResource(lpRsrc);
GlobalFree(m_hMem);
return TRUE;
}

方法一:方便
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();