• 1063阅读
  • 1回复

19.VC(ui)-加载图片资源的方法(随手代码) [复制链接]

上一主题 下一主题
离线啊冲
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2016-02-16




[cpp] view plain copy
  1. BOOL CImageManager::LoadResImage(UINT nResID,  
  2.                                  LPCTSTR lpType,  
  3.                                  Bitmap *&lpImage,  
  4.                                  HINSTANCE hInstance)  
  5. {  
  6.     lpImage  = NULL;  
  7.   
  8.     hInstance = (NULL == hInstance) ? ::AfxGetResourceHandle() : hInstance;  
  9.     // If bmp,use system load.  
  10.     if (RT_BITMAP == lpType)  
  11.     {  
  12.         HBITMAP hBmp = ::LoadBitmap(hInstance, MAKEINTRESOURCE(nResID));  
  13.         lpImage = Bitmap::FromHBITMAP(hBmp, 0);  
  14.         ::DeleteObject(hBmp);  
  15.   
  16.         if (!lpImage)  
  17.         {  
  18.             TRACE("lpImage is NULL\n");  
  19.             return FALSE;  
  20.         }  
  21.         else  
  22.         {  
  23.             if (Gdiplus::Ok != lpImage->GetLastStatus())  
  24.             {  
  25.                 TRACE("lpImage is error\n");  
  26.                 return FALSE;  
  27.             }  
  28.             else  
  29.             {  
  30.                 return TRUE;  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     // User-Defined  
  36.   
  37.     HRSRC hRsrc = ::FindResource(hInstance, MAKEINTRESOURCE(nResID), lpType);  
  38.     if (!hRsrc)  
  39.     {  
  40.         TRACE("hRscs is NULL\n");  
  41.         return FALSE;  
  42.     }  
  43.   
  44.     // Load resource into memory ---------------------------------  
  45.     DWORD len = ::SizeofResource(hInstance, hRsrc);  
  46.   
  47.     BYTE *lpRsrc = (BYTE*)::LoadResource(hInstance, hRsrc);  
  48.     if (!lpRsrc)  
  49.     {  
  50.         TRACE("lpRsrc is NULL\n");  
  51.         return FALSE;  
  52.     }  
  53.   
  54.     // Allocate global memory on which to create stream ----------  
  55.     HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, len);  
  56.   
  57.     BYTE *pMem = (BYTE*)::GlobalLock(hMem);  
  58.     memcpy(pMem, lpRsrc, len);  
  59.   
  60.     IStream *pStream = NULL;  
  61.     // You can query MSDN,why I use TRUE, -- hgy notes.  
  62.     HRESULT ht = ::CreateStreamOnHGlobal(hMem, TRUE, &pStream);  
  63.     if (S_OK != ht)  
  64.     {  
  65.         TRACE("ht is error\n");  
  66.         return FALSE;  
  67.     }  
  68.   
  69.     // Load from stream -------------------------------------------  
  70.     lpImage = Gdiplus::Bitmap::FromStream(pStream);  
  71.   
  72.     // free/release stuff -----------------------------------------  
  73.     ::GlobalUnlock(hMem);  
  74.     pStream->Release();  
  75.     ::FreeResource(lpRsrc);  
  76.   
  77.     if (!lpImage)  
  78.     {  
  79.         TRACE("lpImage is NULL\n");  
  80.         return FALSE;  
  81.     }  
  82.     else  
  83.     {  
  84.         if (Gdiplus::Ok != lpImage->GetLastStatus())  
  85.         {  
  86.             TRACE("lpImage is error\n");  
  87.             return FALSE;  
  88.         }  
  89.         else  
  90.         {  
  91.             return TRUE;  
  92.         }  
  93.     }  
  94. }  


善者 慈悲心常在 无怨无恨 以苦为乐
默认压缩密码www.hifyl.com
文件分享密码问题:http://www.hifyl.com/read-htm-tid-4444.html
离线v2680267313

只看该作者 沙发  发表于: 2016-04-30
用户被禁言,该主题自动屏蔽!
快速回复
限100 字节
批量上传需要先选择文件,再选择上传
 
上一个 下一个