Get the 48×48 or 256×256 icon of a file on Windows

Getting the 16×16 and 32×32 icons on Windows is relatively easy and is often as simple as one call to ExtractIconEx.However, getting the extra large (48×48) and jumbo (256×256) icons introduced respectively by XP and Vista is slighly more complex. This is normally done by:Getting the file information, in particular the icon index, for the given file using SHGetFileInfoRetrieving the system image list where all the icons are storedCasting the image list to an IImageList interface and getting the icon from thereBelow is the code I’m using in Appetizer to retrieve the extra large icon. If needed it can easily be adapted to get the jumbo icon.Update: To do the same thing in C#, see the link in the comments below.view sourceprint?

01.
#include <shlobj.h>
02.
#include <shlguid.h>
03.
#include <shellapi.h>
04.
#include <commctrl.h>
05.
#include <commoncontrols.h>
06.
 
07.
// Get the icon index using SHGetFileInfo
08.
SHFILEINFOW sfi = {0};
09.
SHGetFileInfo(filePath, -1, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX);
10.
 
11.
// Retrieve the system image list.
12.
// To get the 48x48 icons, use SHIL_EXTRALARGE
13.
// To get the 256x256 icons (Vista only), use SHIL_JUMBO
14.
HIMAGELIST* imageList;
15.
HRESULT hResult = SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&imageList);
16.
 
17.
if (hResult == S_OK) {
18.
// Get the icon we need from the list. Note that the HIMAGELIST we retrieved
19.
// earlier needs to be casted to the IImageList interface before use.
20.
HICON hIcon;
21.
hResult = ((IImageList*)imageList)->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hIcon);
22.
 
23.
if (hResult == S_OK) {
24.
// Do something with the icon here.
25.
// For example, in wxWidgets:
26.
wxIcon* icon = new wxIcon();
27.
icon->SetHICON((WXHICON)hIcon);
28.
icon->SetSize(48, 48);
29.
}
30.
}

轉載自 http://pogopixels.com/blog/getting-the-48x48-or-256x256-icon-of-a-file-on-windows/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章