【问题标题】:How can I get large icons for a file extension using Windows shell?如何使用 Windows shell 获取文件扩展名的大图标?
【发布时间】:2012-01-27 13:48:43
【问题描述】:

我找到了各种关于获取文件甚至文件扩展名的系统映像的文章。我有以下方法可用于获取小 16x16 和大 32x32 图像。

    // DLL Import
    [DllImport("shell32")]
    private static extern IntPtr SHGetFileInfo(
        string pszPath,
        uint dwFileAttributes,
        ref SHFILEINFO psfi,
        uint cbFileInfo,
        uint uFlags);

    // Constants/Enums
    private const int FILE_ATTRIBUTE_NORMAL = 0x80;

    private enum SHGetFileInfoConstants : int
    {
        SHGFI_ICON = 0x100,                // get icon
        SHGFI_DISPLAYNAME = 0x200,         // get display name
        SHGFI_TYPENAME = 0x400,            // get type name
        SHGFI_ATTRIBUTES = 0x800,          // get attributes
        SHGFI_ICONLOCATION = 0x1000,       // get icon location
        SHGFI_EXETYPE = 0x2000,            // return exe type
        SHGFI_SYSICONINDEX = 0x4000,       // get system icon index
        SHGFI_LINKOVERLAY = 0x8000,        // put a link overlay on icon
        SHGFI_SELECTED = 0x10000,          // show icon in selected state
        SHGFI_ATTR_SPECIFIED = 0x20000,    // get only specified attributes
        SHGFI_LARGEICON = 0x0,             // get large icon
        SHGFI_SMALLICON = 0x1,             // get small icon
        SHGFI_OPENICON = 0x2,              // get open icon
        SHGFI_SHELLICONSIZE = 0x4,         // get shell size icon
        SHGFI_PIDL = 0x8,                  // pszPath is a pidl
        SHGFI_USEFILEATTRIBUTES = 0x10,    // use passed dwFileAttribute
        SHGFI_ADDOVERLAYS = 0x000000020,   // apply the appropriate overlays
        SHGFI_OVERLAYINDEX = 0x000000040   // Get the index of the overlay
    }

    public static Icon GetSmallIconForExtension(string extension)
    {
        // Get the small icon and clone it, as we MUST destroy the handle when we are done.
        SHFILEINFO shinfo = new SHFILEINFO();
        IntPtr ptr = SHGetFileInfo(
            extension,
            FILE_ATTRIBUTE_NORMAL,
            ref shinfo, (uint)Marshal.SizeOf(shinfo),
            (int)(SHGetFileInfoConstants.SHGFI_ICON |
            SHGetFileInfoConstants.SHGFI_SMALLICON |
            SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES |
            SHGetFileInfoConstants.SHGFI_TYPENAME));
        Icon icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon;
        DestroyIcon(shinfo.hIcon);
        return icon;
    }

    public static Icon GetLargeIconForExtension(string extension)
    {
        // Get the small icon and clone it, as we MUST destroy the handle when we are done.
        SHFILEINFO shinfo = new SHFILEINFO();
        IntPtr ptr = SHGetFileInfo(
            extension,
            FILE_ATTRIBUTE_NORMAL,
            ref shinfo, (uint)Marshal.SizeOf(shinfo),
            (int)(
            SHGetFileInfoConstants.SHGFI_ICON |
            SHGetFileInfoConstants.SHGFI_LARGEICON |
            SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES |
            SHGetFileInfoConstants.SHGFI_TYPENAME
            ));
        Icon icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon;
        DestroyIcon(shinfo.hIcon);
        return icon;
    }

从上述方法之一获取图标后,我将其转换为位图,如下所示:

Image image = icon.ToBitmap();

在上面指定 SHGetFileInfoConstants.SHGFI_LARGEICON 时,我得到 32x32 大小的位图图像。然而,在 Windows 资源管理器中,当您选择查看 Medium Icons 时,您会获得 48x48 大小的图标。

我想要 48x48 大小的图标(不是 32x32)。我需要做什么?

【问题讨论】:

  • 你试过SHGFI_SHELLICONSIZE吗?只发布真实代码。
  • 我也试过这个:IntPtr ptr = SHGetFileInfo( extension, FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)(SHGetFileInfoConstants.SHGFI_ICON | SHGetFileInfoConstants.SHGFI_SYSICONINDEX | SHGetFileInfoConstants.SHGFI_LARGEICON | SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | SHGetFileInfoConstants.SHGFI_TYPENAME)); 我仍然只有 32x32(顺便说一句,我发布的是我的实际代码)。
  • 这几乎是重复的:stackoverflow.com/questions/6434178/…
  • 如果这是真实代码,那么您需要解释为什么 GetLargeIconForExtension() 使用 SHGFI_SMALLICON :)
  • 当场抓获!我确实复制/粘贴了真实的代码,但随后,证据无可争辩,修改了属性。固定的。 :)

标签: c# winapi shell pinvoke windows-shell


【解决方案1】:

SHGetImageList 的文档中给出了 shell 图标的大小:

  • 16x16 = 小
  • 32x32 = 大
  • 48x48 = 超大
  • 256x256 = 巨型

因此,当您要求“大”图标时,预计会得到 32x32。如果您想要超大图标,则需要从SHIL_EXTRALARGE 图像列表中获取它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2018-11-08
    • 2018-01-14
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多