如何确定 SHGetImageList 中使用的桌面和网络图标索引?

发布于 2024-12-29 05:55:46 字数 1590 浏览 1 评论 0原文

我能够使用下面包含的 API 成功提取文件系统驱动器、文件夹和文件的图标。有关 DLL 导入等的其他信息(帮助我走到这一步)可以找到 此处。通过调用方法 GetExtraLargeIconForFolder 我在图标中获得了一个 48x48 大小的图像。

public enum ImageListIconSize : int
{
    Large = 0x0,
    Small = 0x1,
    ExtraLarge = 0x2,
    Jumbo = 0x4
}

private static IImageList GetSystemImageListHandle(ImageListIconSize size)
{
    IImageList iImageList;
    Guid imageListGuid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
    int ret = SHGetImageList(
        (int)size,
        ref imageListGuid,
        out iImageList
        );
    return iImageList;
}

public static Icon GetExtraLargeIconForFolder(string path)
{
    SHFILEINFO shinfo = new SHFILEINFO();
    IntPtr retVal = SHGetFileInfo(
        path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
        (int)(SHGetFileInfoConstants.SHGFI_SYSICONINDEX |
              SHGetFileInfoConstants.SHGFI_ICON));

    int iconIndex = shinfo.iIcon;
    IImageList iImageList =
        (IImageList)GetSystemImageListHandle(ImageListIconSize.ExtraLarge);
    IntPtr hIcon = IntPtr.Zero;
    if (iImageList != null)
    {
        iImageList.GetIcon(iconIndex,
            (int)ImageListDrawItemConstants.ILD_TRANSPARENT, ref hIcon);
    }

    Icon icon = null;
    if (hIcon != IntPtr.Zero)
    {
        icon = Icon.FromHandle(hIcon).Clone() as Icon;
        DestroyIcon(shinfo.hIcon);
    }
    return icon;
}

在 Windows 资源管理器中,可以看到桌面、网络和计算机的图标。如何获取这些文件系统节点的正确图标索引?

I am able to successfully extract the icons for file system drives, folders and files using the APIs I included below. Additional info on the DLL imports etc. that helped me get this far can be found here. By calling the method GetExtraLargeIconForFolder I get a 48x48 sized image in the icon.

public enum ImageListIconSize : int
{
    Large = 0x0,
    Small = 0x1,
    ExtraLarge = 0x2,
    Jumbo = 0x4
}

private static IImageList GetSystemImageListHandle(ImageListIconSize size)
{
    IImageList iImageList;
    Guid imageListGuid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
    int ret = SHGetImageList(
        (int)size,
        ref imageListGuid,
        out iImageList
        );
    return iImageList;
}

public static Icon GetExtraLargeIconForFolder(string path)
{
    SHFILEINFO shinfo = new SHFILEINFO();
    IntPtr retVal = SHGetFileInfo(
        path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
        (int)(SHGetFileInfoConstants.SHGFI_SYSICONINDEX |
              SHGetFileInfoConstants.SHGFI_ICON));

    int iconIndex = shinfo.iIcon;
    IImageList iImageList =
        (IImageList)GetSystemImageListHandle(ImageListIconSize.ExtraLarge);
    IntPtr hIcon = IntPtr.Zero;
    if (iImageList != null)
    {
        iImageList.GetIcon(iconIndex,
            (int)ImageListDrawItemConstants.ILD_TRANSPARENT, ref hIcon);
    }

    Icon icon = null;
    if (hIcon != IntPtr.Zero)
    {
        icon = Icon.FromHandle(hIcon).Clone() as Icon;
        DestroyIcon(shinfo.hIcon);
    }
    return icon;
}

In Windows Explorer one sees, icons for the Desktop, Network and Computer. How does one go about getting the correct icon index for these file system nodes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2025-01-05 05:55:46

你已经快到了。您仍然使用 SHGetFileInfo,但需要在 flags 参数中传递 SHGFI_PIDL

然后,您需要通过传递 PIDL 而不是路径来指定感兴趣的 shell 对象。通过调用 获取 PIDL SHGetSpecialFolderLocation。传递 CSIDL 值到此例程,例如 CSIDL_DESKTOPCSIDL_DRIVESCSIDL_NETWORK 等。

You are nearly there. You still use SHGetFileInfo but instead you will need to pass SHGFI_PIDL in the flags parameter.

Then you need to specify the shell object of interest by passing a PIDL rather than a path. Obtain the PIDL by calling SHGetSpecialFolderLocation. Pass a CSIDL value to this routine, e.g. CSIDL_DESKTOP, CSIDL_DRIVES, CSIDL_NETWORK etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文