• 加载图片资源的方法


     
    BOOL CImageManager::LoadResImage(UINT nResID,
    								 LPCTSTR lpType,
    								 Bitmap *&lpImage, 
    								 HINSTANCE hInstance)
    {
    	lpImage  = NULL;
    
    	hInstance = (NULL == hInstance) ? ::AfxGetResourceHandle() : hInstance;
    	// If bmp,use system load.
    	if (RT_BITMAP == lpType)
    	{
    		HBITMAP hBmp = ::LoadBitmap(hInstance, MAKEINTRESOURCE(nResID));
    		lpImage = Bitmap::FromHBITMAP(hBmp, 0);
    		::DeleteObject(hBmp);
    
    		if (!lpImage)
    		{
    			TRACE("lpImage is NULL\n");
    			return FALSE;
    		}
    		else
    		{
    			if (Gdiplus::Ok != lpImage->GetLastStatus())
    			{
    				TRACE("lpImage is error\n");
    				return FALSE;
    			}
    			else
    			{
    				return TRUE;
    			}
    		}
    	}
    
    	// User-Defined
    
    	HRSRC hRsrc = ::FindResource(hInstance, MAKEINTRESOURCE(nResID), lpType);
    	if (!hRsrc)
    	{
    		TRACE("hRscs is NULL\n");
    		return FALSE;
    	}
    
    	// Load resource into memory ---------------------------------
    	DWORD len = ::SizeofResource(hInstance, hRsrc);
    
    	BYTE *lpRsrc = (BYTE*)::LoadResource(hInstance, hRsrc);
    	if (!lpRsrc)
    	{
    		TRACE("lpRsrc is NULL\n");
    		return FALSE;
    	}
    
    	// Allocate global memory on which to create stream ----------
    	HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, len);
    
    	BYTE *pMem = (BYTE*)::GlobalLock(hMem);
    	memcpy(pMem, lpRsrc, len);
    
    	IStream *pStream = NULL;
    	// You can query MSDN,why I use TRUE, -- hgy notes.
    	HRESULT ht = ::CreateStreamOnHGlobal(hMem, TRUE, &pStream);
    	if (S_OK != ht)
    	{
    		TRACE("ht is error\n");
    		return FALSE;
    	}
    
    	// Load from stream -------------------------------------------
    	lpImage = Gdiplus::Bitmap::FromStream(pStream);
    
    	// free/release stuff -----------------------------------------
    	::GlobalUnlock(hMem);
    	pStream->Release();
    	::FreeResource(lpRsrc);
    
    	if (!lpImage)
    	{
    		TRACE("lpImage is NULL\n");
    		return FALSE;
    	}
    	else
    	{
    		if (Gdiplus::Ok != lpImage->GetLastStatus())
    		{
    			TRACE("lpImage is error\n");
    			return FALSE;
    		}
    		else
    		{
    			return TRUE;
    		}
    	}
    }
    
    自己写的,经常用到,记录下,总不能每次都重写吧。
  • 相关阅读:
    linux内核中创建线程方法
    高等数学所有符号的写法与读法
    Git学习笔记03--git reset
    git status message
    [小技巧] git: Your branch and 'origin/master' have diverged
    phalcon: 资源文件管 理 引入css,js
    phalcon: model 验证数据完整性
    phalcon:model 事件与事件管理器
    phalcon: 查找记录(Finding Records)可用的查询设置如下:
    phalcon count统计
  • 原文地址:https://www.cnblogs.com/hgy413/p/3693566.html
Copyright © 2020-2023  润新知