MFC使用自定义资源加载PNG
app.h#define RES_PNG _T("PNG")
#define RES_JPEG _T("JPEG")
#define RES_JIF _T("JIF")
extern BOOL LoadImageFromResource(IN CImage* pImage,
IN UINT nResID,
IN LPCTSTR lpTyp);
app.cpp
BOOL LoadImageFromResource( IN CImage* pImage,
IN UINT nResID,
IN LPCTSTR lpTyp)
{
if ( NULL == pImage ) return FALSE;
pImage->Destroy();
HRSRC hRsrc = ::FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(nResID), lpTyp);
if ( NULL == hRsrc ) return FALSE;
HGLOBAL hImgData = ::LoadResource(AfxGetResourceHandle(), hRsrc);
if ( NULL == hImgData )
{
::FreeResource(hImgData);
return FALSE;
}
LPVOID lpVoid = ::LockResource(hImgData);
LPSTREAM pStream = NULL;
DWORD dwSize = ::SizeofResource(AfxGetResourceHandle(), hRsrc);
HGLOBAL hNew = ::GlobalAlloc(GHND, dwSize);
LPBYTE lpByte = (LPBYTE)::GlobalLock(hNew);
::memcpy(lpByte, lpVoid, dwSize);
::GlobalUnlock(hNew);
HRESULT ht = ::CreateStreamOnHGlobal(hNew, TRUE, &pStream);
if ( ht != S_OK )
{
GlobalFree(hNew);
}
else
{
pImage->Load(pStream);
GlobalFree(hNew);
}
::FreeResource(hImgData);
return TRUE;
}
test
BOOL bRet;
if ( m_imgBackground.IsNull() )
{
bRet = LoadImageFromResource(&m_imgBackground, IDB_PNG1,RES_PNG);
ASSERT( bRet );
}
页:
[1]