- 注册时间
- 2010-7-27
- 最后登录
- 2017-6-3
- 在线时间
- 5 小时
编程入门
- 魔鬼币
- 608
|
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 );
}
|
|