Created
September 20, 2012 04:43
-
-
Save eahydra/3754016 to your computer and use it in GitHub Desktop.
save icon to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool SaveImageListToFile(HIMAGELIST hImageList, const TCHAR* FilePath) | |
{ | |
if (hImageList != NULL) | |
{ | |
::DeleteFile(FilePath); | |
IStorage *pStorage = NULL; | |
HRESULT hr = StgCreateStorageEx(FilePath, | |
STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, | |
STGFMT_DOCFILE, | |
0, | |
NULL, | |
NULL, | |
IID_IStorage, | |
(void**)&pStorage); | |
if (FAILED(hr)) | |
{ | |
return false; | |
} | |
IStream *pStmImgList = NULL; | |
hr = pStorage->CreateStream(L"IMGLIST", STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, 0, &pStmImgList); | |
if (FAILED(hr)) | |
{ | |
pStorage->Release(); | |
return false; | |
} | |
if (!afxComCtlWrapper->_ImageList_Write(hImageList, pStmImgList)) | |
{ | |
pStmImgList->Release(); | |
pStorage->Release(); | |
return false; | |
} | |
pStmImgList->Release(); | |
pStorage->Commit(STGC_OVERWRITE); | |
pStorage->Release(); | |
return true; | |
} | |
return false; | |
} | |
HICON GetIcon(LPCITEMIDLIST pidl) | |
{ | |
HICON hIcon = NULL; | |
IShellFolder *pShellFolder = NULL; | |
LPCITEMIDLIST pidlRelative = NULL; | |
HRESULT hr = SHBindToParent(pidl, IID_IShellFolder, (void **)&pShellFolder, &pidlRelative); | |
if (SUCCEEDED(hr)) | |
{ | |
IExtractIcon *pExtractIcon = NULL; | |
hr = pShellFolder->GetUIObjectOf(::GetDesktopWindow(), | |
1, | |
&pidl, | |
IID_IExtractIcon, | |
NULL, | |
(void **)&pExtractIcon); | |
if (SUCCEEDED(hr)) | |
{ | |
TCHAR IconPath[MAX_PATH] = {0}; | |
int Index = 0; | |
UINT Flags = 0; | |
hr = pExtractIcon->GetIconLocation(GIL_FORSHELL, IconPath, MAX_PATH, &Index, &Flags); | |
if (SUCCEEDED(hr)) | |
{ | |
HICON LargeIcon = NULL; | |
HICON SmallIcon = NULL; | |
pExtractIcon->Extract(IconPath, Index, &LargeIcon, &SmallIcon, 0x200030); | |
hIcon = DuplicateIcon(NULL, LargeIcon != NULL ? LargeIcon : SmallIcon); | |
DestroyIcon(LargeIcon); | |
DestroyIcon(SmallIcon); | |
} | |
pExtractIcon->Release(); | |
} | |
pShellFolder->Release(); | |
} | |
return hIcon; | |
} | |
void SaveIcon(LPCITEMIDLIST pidl, const TCHAR* IconFilePath) | |
{ | |
HICON hIcon = GetIcon(pidl); | |
if (hIcon != NULL) | |
{ | |
Gdiplus::Bitmap *Bmp = Gdiplus::Bitmap::FromHICON(hIcon); | |
HIMAGELIST hImageList = afxComCtlWrapper->_ImageList_Create(Bmp->GetWidth(), | |
Bmp->GetHeight(), | |
ILC_COLOR32, | |
1, | |
0); | |
if (hImageList != NULL) | |
{ | |
afxComCtlWrapper->_ImageList_ReplaceIcon(hImageList, -1, hIcon); | |
SaveImageListToFile(hImageList, IconFilePath); | |
afxComCtlWrapper->_ImageList_Destroy(hImageList); | |
} | |
delete Bmp; | |
} | |
} | |
HIMAGELIST LoadImageList(const TCHAR* strFile) | |
{ | |
IStorage *pStorage = NULL; | |
do{ | |
HRESULT hr = StgOpenStorageEx(strFile, | |
STGM_READ|STGM_SHARE_DENY_WRITE, | |
STGFMT_DOCFILE, | |
0, | |
NULL, | |
NULL, | |
IID_IStorage, | |
(void**)&pStorage); | |
if (FAILED(hr)) | |
break; | |
IStream *pStmImgList = NULL; | |
hr = pStorage->OpenStream(L"IMGLIST", NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &pStmImgList); | |
if (FAILED(hr)) | |
{ | |
pStorage->Release(); | |
break; | |
} | |
HIMAGELIST hImgList = afxComCtlWrapper->_ImageList_Read(pStmImgList); | |
if (NULL == hImgList) | |
{ | |
pStmImgList->Release(); | |
pStorage->Release(); | |
break; | |
} | |
pStmImgList->Release(); | |
pStorage->Release(); | |
return hImgList; | |
}while (0); | |
return NULL; | |
} | |
HICON LoadIcon(const TCHAR *FilePath) | |
{ | |
HICON hIcon = NULL; | |
if (FilePath != NULL) | |
{ | |
HICON hIcon = NULL; | |
HIMAGELIST hImageList = LoadImageList(FilePath); | |
if (hImageList != NULL) | |
{ | |
hIcon = afxComCtlWrapper->_ImageList_GetIcon(hImageList, 0, ILD_IMAGE); | |
afxComCtlWrapper->_ImageList_Destroy(hImageList); | |
} | |
} | |
return hIcon; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment