我无法让 SHGetFileInfo 返回图标位置 [英] I can't get SHGetFileInfo to return an icon location
问题描述
我使用的是 Windows 10 Pro 和 Visual Studio 2013,并且我正在使用 SHGetFileInfoW
获取文件类型的图标位置(路径 + 索引):
I'm on Windows 10 Pro and Visual Studio 2013, and I'm using SHGetFileInfoW
to get an icon location (path + index) for a file type:
std::wstring wFile { L"a.bas" };
SHFILEINFOW fi {};
DWORD success = ::SHGetFileInfoW(wFile.c_str(),
FILE_ATTRIBUTE_NORMAL,
&fi,
sizeof(fi),
SHGFI_USEFILEATTRIBUTES | SHGFI_ICONLOCATION);
无论wFile
是指现有文件还是任何文件名,调用都会返回1 表示成功.fi
的 iIcon
成员设置为数字,但 szDisplayString
为空.不只是驱动器号被 \0
覆盖(似乎发生了 here) 但它完全充满了 \0
.
No matter whether wFile
refers to an existing file or is just any filename, the call returns 1 indicating success. The iIcon
member of fi
is set to a number, but szDisplayString
is empty. Not just the drive letter is overwritten with \0
(as seemed to happen here) but it is completely filled with \0
.
Microsoft 建议使用 <代码>IExtractIcon::
GetIconLocation
作为替代,但我需要获取不在本地文件系统上的文件的图标,所以我可以't 得到一个 IShellInfo
对象,它会得到这个接口指针.
Microsoft recommends using IExtractIcon
::
GetIconLocation
as an alternative, but I need to get the icon for files which are not on a local filesystem, so I can't get an IShellInfo
object which would get me this interface pointer.
另一方面,获取图标句柄是有效的.这个功能只是有问题还是我做错了什么?有解决方法吗?
Getting an icon handle works, on the other hand. Is this function just buggy or am I doing something wrong? Is there a workaround?
推荐答案
图标可以动态生成并且可能不会暴露其图像的路径.图标处理程序通过在其 IExtractIcon::GetIconLocation
实现.如果设置了 GIL_SIMULATEDOC
,shell 通常还必须动态生成一个图标.
Icons can be dynamically generated and might not expose the path to its images. Icon handlers communicate this to the shell by setting the GIL_NOTFILENAME
flag in their IExtractIcon::GetIconLocation
implementation. If GIL_SIMULATEDOC
is set the shell must also typically generate a icon on the fly.
如果您调用 SHGetFileInfo
并设置了 SHGFI_SELECTED
标志,那么无论您要求哪种文件类型,函数都可能必须生成一个新图标.
If you call SHGetFileInfo
with the SHGFI_SELECTED
flag set then then function probably has to generate a new icon no matter which file type you are asking for.
如果您在 ListView/TreeView 中显示文件列表,则通常使用 SHGFI_SYSICONINDEX|SHGFI_SHELLICONSIZE|SHGFI_SMALLICON
并使用系统图像列表.
If you are displaying a file list in a ListView/TreeView then you typically use SHGFI_SYSICONINDEX|SHGFI_SHELLICONSIZE|SHGFI_SMALLICON
and use the system image list.
如果您需要 HICON,请使用 SHGFI_ICON
.
Use SHGFI_ICON
if you need a HICON.
如果指定了 SHGFI_ICONLOCATION
,则 SHGetFileInfo
使用 IExtractIcon:
If SHGFI_ICONLOCATION
is specified then SHGetFileInfo
uses IExtractIcon:
检索包含表示由 pszPath 指定的文件的图标的文件的名称,该名称由文件图标处理程序的 IExtractIcon::GetIconLocation 方法返回.
Retrieve the name of the file that contains the icon representing the file specified by pszPath, as returned by the IExtractIcon::GetIconLocation method of the file's icon handler.
这篇关于我无法让 SHGetFileInfo 返回图标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!