基本信息
源码名称:提取应用程序ICON图标例子(得到exe图标并保存png)
源码大小:23.95M
文件格式:.zip
开发语言:C/C++
更新时间:2015-06-29
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍

#pragma once

#include <gdiplus.h>

namespace iconhelper
{
	int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
	{
		UINT  num = 0;          // number of image encoders
		UINT  size = 0;         // size of the image encoder array in bytes

		Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;

		Gdiplus::GetImageEncodersSize(&num, &size);
		if(size == 0)
			return -1;  // Failure

		pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
		if(pImageCodecInfo == NULL)
			return -1;  // Failure

		GetImageEncoders(num, size, pImageCodecInfo);

		for(UINT j = 0; j < num;   j)
		{
			if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
			{
				*pClsid = pImageCodecInfo[j].Clsid;
				free(pImageCodecInfo);
				return j;  // Success
			}    
		}

		free(pImageCodecInfo);
		return -1;  // Failure
	}

	bool SaveHIcon2PngFile(HICON hIcon, LPCTSTR lpszPicFileName)
	{  
		if (hIcon == NULL)
		{
			return false;
		}

		ICONINFO icInfo = { 0 };	
		if (!::GetIconInfo(hIcon, &icInfo))
		{
			return false;
		}

		BITMAP bitmap; 
		GetObject(icInfo.hbmColor, sizeof(BITMAP), &bitmap);

		Gdiplus::Bitmap* pBitmap = NULL;
		Gdiplus::Bitmap* pWrapBitmap = NULL;
		if (bitmap.bmBitsPixel != 32) 
		{   
			pBitmap = Gdiplus::Bitmap::FromHICON(hIcon); 
		} 
		else
		{
			pWrapBitmap = Gdiplus::Bitmap::FromHBITMAP(icInfo.hbmColor, NULL);
			if (!pWrapBitmap)
				return false;

			Gdiplus::BitmapData bitmapData;
			Gdiplus::Rect rcImage(0, 0, pWrapBitmap->GetWidth(), pWrapBitmap->GetHeight());
			pWrapBitmap->LockBits(&rcImage, Gdiplus::ImageLockModeRead, pWrapBitmap->GetPixelFormat(), &bitmapData); 
			pBitmap = new Gdiplus::Bitmap(bitmapData.Width, bitmapData.Height, bitmapData.Stride, PixelFormat32bppARGB, (BYTE*)bitmapData.Scan0);
			pWrapBitmap->UnlockBits(&bitmapData);
		}

		CLSID encoderCLSID;
		GetEncoderClsid(_T("image/png"), &encoderCLSID);
		Gdiplus::Status st= pBitmap->Save(lpszPicFileName ,&encoderCLSID, NULL/*&encoderParameters*/);
		if( st != Gdiplus::Ok )
			return false;

		delete pBitmap; 
		if (pWrapBitmap)
			delete pWrapBitmap;
		DeleteObject(icInfo.hbmColor); 
		DeleteObject(icInfo.hbmMask);  

		return true;
	}

	// 调用方负责DestroyIcon HICON
	HICON GetFileIcon(const CString& strFilePath, BOOL bLarge)
	{
// 		SHFILEINFO SHFI;
// 		ZeroMemory(&SHFI, sizeof(SHFI));
// 		DWORD_PTR ret = ::SHGetFileInfo(strFilePath, 0, &SHFI, sizeof(SHFI), 
// 			SHGFI_ICON | (bLarge ? SHGFI_LARGEICON : SHGFI_SMALLICON));
// 
// 		if (ret != 0)
// 		{
// 			return SHFI.hIcon;
// 		}

		CString strTemp = strFilePath;

		SHFILEINFOW sfi = {0};
		SHGetFileInfo(strTemp.GetBuffer(0), -1, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX);

		// Retrieve the system image list.
		// To get the 48x48 icons, use SHIL_EXTRALARGE
		// To get the 256x256 icons (Vista only), use SHIL_JUMBO
		IImageList* imageList;

		HRESULT hResult = SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&imageList);
		//SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&imageList);

		if (hResult == S_OK)
		{
			// Get the icon we need from the list. Note that the HIMAGELIST we retrieved
			// earlier needs to be casted to the IImageList interface before use.
			HICON hIcon;
			hResult = (imageList)->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hIcon);

			if (hResult == S_OK)
			{
				// Do something with the icon here.
				return hIcon;
			}
		}

		return NULL;
	}

	BOOL SaveFileIcon(const CString& strFilePath, const CString& strImagePath, BOOL bLarge = TRUE)
	{
		BOOL bRet = FALSE;
		HICON hIcon = GetFileIcon(strFilePath, bLarge);
		if (hIcon != NULL)
		{
			bRet = SaveHIcon2PngFile(hIcon, strImagePath);
			::DestroyIcon(hIcon);
		}

		return bRet;
	}
}