- SHGetFileInfo 获取选中文件小图标
SHGetFileInfoA(szFile, FILE_ATTRIBUTE_DIRECTORY, &info, sizeof(info), SHGFI_ICON | SHGFI_DISPLAYNAME)
- 通过SHGetImageList,ImageList_GetIcon 获取对应的大图标
HICON CMainWnd::GetDefaultIcon(LPCTSTR szFile)
{
static bool bInit = false;
static IImageList *imageList = NULL;
if (!bInit && imageList == NULL)
{
bInit = true;
IID IID_IImageList = { 0 };
HRESULT hr = IIDFromString(L"{46EB5926-582E-4017-9FDF-E8998DAA0950}", &IID_IImageList);
ASSERT(SUCCEEDED(hr));
SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void **)(&imageList));
}
SHFILEINFOA info = { 0 };
if (SHGetFileInfoA(szFile, FILE_ATTRIBUTE_DIRECTORY, &info, sizeof(info), SHGFI_ICON | SHGFI_DISPLAYNAME))
{
HICON hIcon = info.hIcon;
if (imageList)
{
hIcon = ImageList_GetIcon((HIMAGELIST)imageList, info.iIcon, 0);
if (hIcon)
DestroyIcon(info.hIcon);
else
hIcon = info.hIcon;
}
return hIcon;
}
return NULL;
}
包含:Shellapi.h Commctrl.h comctl32.lib
本文介绍如何使用Windows Shell API中的SHGetFileInfo和SHGetImageList函数来获取文件的小图标和大图标。通过示例代码展示了如何初始化图像列表,以及如何从选中的文件获取图标信息。
1516

被折叠的 条评论
为什么被折叠?



