当前位置:首页 > 资讯 > 正文

C++博客

C++博客

CYourClass::~CYourClass()
{
    
for(IMG_VECTOR::iterator it = m_arImage.begin(); it != m_arImage.end(); it++)
        delete 
*it;
    
for(HGLB_VECTOR::iterator it = m_arGlobal.begin(); it != m_arGlobal.end(); it++)
    {
        ::GlobalUnlock(
*it);
        ::GlobalFree(
*it);
    }
}

void CYourClass::AddImage(HMODULE hInst, UINT nResourceID, LPCTSTR lpType)
{
    
if(lpType == RT_BITMAP)
    {
        
//GDI+ can not load RT_BITMAP resouce, 
        
//because they are predefined resource, 
        
//they don't contains the image file header.
        assert(FALSE);
        
return;
    }

    HRSRC hResource 
= ::FindResource(hInst, MAKEINTRESOURCE(nResourceID), lpType);
    
if (!hResource)
        
return;

    DWORD imageSize 
= ::SizeofResource(hInst, hResource);
    
if (!imageSize)
        
return;

    
const void* pResourceData = ::LockResource(::LoadResource(hInst, hResource));
    
if (!pResourceData)
        
return;

    HGLOBAL hBuffer 
= ::GlobalAlloc(GMEM_FIXED, imageSize);
    
if (NULL == hBuffer)
        
return;

    
void* pBuffer = ::GlobalLock(hBuffer);
    
if (pBuffer)
    {
        CopyMemory(pBuffer, pResourceData, imageSize);
        IStream
* pStream = NULL;
        
if (::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream) == S_OK)
        {
            Gdiplus::Image 
* pImage = Gdiplus::Image::FromStream(pStream);
            pStream
->Release();
            
if (pImage)
            { 
                
if (pImage->GetLastStatus() == Gdiplus::Ok &&
                    pImage
->GetWidth() > 0)
                {
                    m_arImage.push_back(pImage);
                    
//it seems the image will take usage of the global memory.
                    
//so the global memory should be kept until the image destroy.
                    m_arGlobal.push_back(hBuffer);
                    
return;
                }

                delete pImage;
            }
        }
        ::GlobalUnlock(hBuffer);
    }
    ::GlobalFree(hBuffer);