【问题标题】:What is the Maximum Size Of An Icon Returned From SHGetFileInfo()?从 SHGetFileInfo() 返回的图标的最大大小是多少?
【发布时间】:2015-01-18 16:48:24
【问题描述】:

使用 SHGetFileInfo() 函数,我可以返回的最大图标大小是多少?就函数状态而言,我可以取回一个 32x32 像素的图标(AKA SHGFI_LARGEICON)。但我试图弄清楚是否有办法获得更大的东西,比如 48x48 像素图标。

我发现有像...这样的常量

public const uint SHGFI_LARGEICON = 0x000000000;     // get large icon
public const uint SHGFI_SMALLICON = 0x000000001;     // get small icon
public const uint SHIL_JUMBO = 0x000000004;     // get jumbo icon 256x256
public const uint SHIL_EXTRALARGE = 0x000000002;     // get extra large icon 48x48
public const uint SHIL_LARGE = 0x000000000;     // get large icon 32x32
public const uint SHIL_SMALL = 0x000000001;     // get small icon 16x16
public const uint SHIL_SYSSMALL = 0x000000003;     // get icon based off of GetSystemMetrics

...但我不确定这些是否对 SHGetFileInfo() 有效。我试过了,图标看起来很模糊而且不对。 (在您使用“视图:中等图标”设置时,它们看起来不像 Windows 资源管理器中的那样清晰/漂亮)

这就是我所拥有的(注意:这不是一个可行的解决方案, SHIL 值未记录在 SHGetFileInfo() 函数中。这只是我试一试的东西。)...

public const uint SHIL_JUMBO = 0x000000004;     // get jumbo icon 256x256
public const uint SHIL_EXTRALARGE = 0x000000002;     // get extra large icon 48x48
public const uint SHIL_LARGE = 0x000000000;     // get large icon 32x32
public const uint SHIL_SMALL = 0x000000001;     // get small icon 16x16
public const uint SHIL_SYSSMALL = 0x000000003;     // get icon based off of GetSystemMetrics
public const uint SHGFI_ICON = 0x000000100;     // get icon
public const uint SHGFI_OPENICON = 0x000000002;     // get open icon

[DllImport("Shell32.dll")]
public static extern IntPtr SHGetFileInfo(
    IntPtr pszPath,
    uint dwFileAttributes,
    ref SHFILEINFO psfi,
    uint cbFileInfo,
    uint uFlags
);
[DllImport("User32.dll")]
public static extern int DestroyIcon(IntPtr hIcon);

public struct SHFILEINFO
{
    public const int NAMESIZE = 80;
    public IntPtr hIcon;
    public int iIcon;
    public uint dwAttributes;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
    public string szDisplayName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)]
    public string szTypeName;
}

public enum IconSize
{
    Jumbo = 4, //256x256
    ExtraLarge = 2, //48x48
    Large = 0, //32x32
    Small = 1 //16x16
}

IconSize size = IconSize.ExtraLarge;

uint flags = SHGFI_ICON;
flags |= SHGFI_OPENICON;
switch (size)
{
    case IconSize.Small:
        flags |= SHIL_SMALL;
        break;
    case IconSize.Large:
        flags |= SHIL_LARGE;
        break;
    case IconSize.ExtraLarge:
        flags |= SHIL_EXTRALARGE;
        break;
    case IconSize.Jumbo:
        flags |= SHIL_JUMBO;
        break;
}

//Get me a PDIL to the My Documents folder (this is done with a LOT of other
//code but I know for a fact it returns the name, path, and PDIL correctly!
CGFolder cFolder = new CGFolder(Environment.SpecialFolder.MyDocuments);

string sName = cFolder.Pidl.DisplayName;   
string sPath = cFolder.Pidl.PhysicalPath;
IntPtr ptrPDIL = cFolder.Pidl.Pidl;
SHFILEINFO shfi = new SHFILEINFO();
SHGetFileInfo(ptrPDIL, 0, ref shfi, (uint)Marshal.SizeOf(shfi), flags);

if (shfi.hIcon == IntPtr.Zero) return null;

icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

DestroyIcon(shfi.hIcon);

return icon;

参考资料:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179%28v=vs.85%29.aspx)

【问题讨论】:

  • 很难猜出为什么您认为这些 SHIL 值与函数有任何关系。您链接的 MSDN 文章列出了有效的 SHGFI 值。
  • 正确,我能在 atm 中获得的最大图标是 32x32 大小的图标。我的例子只是我试图做的事情。我在另一个问题上找到了一些示例,但对此几乎没有解释……所以我尝试了。但我认为它可能起作用的唯一原因是因为 LARGE 和 SMALL 的值是相同的......我想这到底是怎么回事。现在它没有按预期工作,我想我会问它可以从函数返回的最大可能图标大小。

标签: c# winapi icons pinvoke dllimport


【解决方案1】:

系统图像列表中的索引对于所有图标大小都是相同的,因此使用带有SHGFI_SYSICONINDEX 标志的SHGetFileInfo 获取索引,并使用imagelist API 从“超大”或您可以从 SHGetImageList 函数中获取的“巨型”图像列表。

SHGetFileInfo 本身只能返回小(例如 16x16)和大(例如 32x32)HICON。

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 2019-04-23
    • 2013-02-26
    • 2012-06-30
    • 2011-03-13
    • 1970-01-01
    • 2013-03-02
    • 2012-11-10
    • 2011-02-28
    相关资源
    最近更新 更多