实例介绍
【实例截图】
【核心代码】
#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; } }
标签: 图标
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论