如何从 C# 代码中获取 Visual Studio 文件类型图标作为位图

How to get Visual Studio file type icons as Bitmap from C# code

目前我正在研究 VS Closed Document Reopen extension,它的工作方式与在 Web 浏览器中的工作方式类似。使用热键重新打开上次关闭的 "tab":Ctrl+Shift+T

我还添加了一个工具 Window。用户可以在其中查看完整的已关闭文档历史记录。我想添加到此 WPF window 的是文件类型在 VS 中的实际图标。不是在 Windows 中注册到这些扩展的图标。但是 解决方案资源管理器窗格中显示的实际图标。我想让它们从 C# 代码中获取 Icon/Bitmap/byte[] 任何东西。所以我可以将它转换为 WPF BitmapSource 并将其动态绑定到 ListView。所以我不想从互联网上下载 VS 图标...

DTE2对象的接口有点绕,不好找。到目前为止,我找不到任何解决方案。知道我该怎么做吗?

您可以使用 Windows API 阅读它。

我在这里找到了解决方案:Source

这是我的版本:(主要针对 .NET 4.7 进行了更新)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Pillage
{
    public static class IconManager
    {
        private static readonly Dictionary<string, ImageSource> smallIconCache = new Dictionary<string, ImageSource>();
        private static readonly Dictionary<string, ImageSource> largeIconCache = new Dictionary<string, ImageSource>();

        public static ImageSource FindIconForFilename(string fileName, bool isLarge)
        {
            var extension = Path.GetExtension(fileName);

            if (extension == null) return null;

            var cache = isLarge ? largeIconCache : smallIconCache;

            if (cache.TryGetValue(extension, out var icon)) return icon;

            icon = IconReader
                .GetFileIcon(fileName, isLarge ? IconReader.IconSize.Large : IconReader.IconSize.Small, false)
                .ToImageSource();
            cache.Add(extension, icon);
            return icon;
        }

        private static ImageSource ToImageSource(this Icon icon)
        {
            var imageSource =
                Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            return imageSource;
        }

        private static class IconReader
        {
            public enum IconSize
            {
                Large = 0,
                Small = 1
            }

            public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
            {
                var shfi = new Shell32.Shfileinfo();
                var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;
                if (linkOverlay) flags += Shell32.ShgfiLinkoverlay;

                if (IconSize.Small == size)
                    flags += Shell32.ShgfiSmallicon;
                else
                    flags += Shell32.ShgfiLargeicon;

                Shell32.SHGetFileInfo(name,
                    Shell32.FileAttributeNormal,
                    ref shfi,
                    (uint) Marshal.SizeOf(shfi),
                    flags);

                var icon = (Icon) Icon.FromHandle(shfi.hIcon).Clone();
                User32.DestroyIcon(shfi.hIcon);

                return icon;
            }
        }

        private static class Shell32
        {
            private const int MaxPath = 256;

            [StructLayout(LayoutKind.Sequential)]
            public struct Shfileinfo
            {
                private const int Namesize = 80;
                public readonly IntPtr hIcon;
                private readonly int iIcon;
                private readonly uint dwAttributes;

                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MaxPath)] private readonly string szDisplayName;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = Namesize)] private readonly string szTypeName;
            }

            public const uint ShgfiIcon = 0x000000100;
            public const uint ShgfiLinkoverlay = 0x000008000;
            public const uint ShgfiLargeicon = 0x000000000;
            public const uint ShgfiSmallicon = 0x000000001;
            public const uint ShgfiUsefileattributes = 0x000000010;
            public const uint FileAttributeNormal = 0x00000080;

            [DllImport("Shell32.dll")]
            public static extern IntPtr SHGetFileInfo(
                string pszPath,
                uint dwFileAttributes,
                ref Shfileinfo psfi,
                uint cbFileInfo,
                uint uFlags
            );
        }

        private static class User32
        {
            [DllImport("User32.dll")]
            public static extern int DestroyIcon(IntPtr hIcon);
        }
    }
}

我用这段开源代码制作了一个程序,您可以在这里找到它:Pillage这是一个文件搜索程序。显示它根据此代码搜索的每个文件的图标。

Pillage

感谢您的回答和评论。不幸的是,他们所有人都提出了一些建议,而我在问题中说过,我不想这样做。喜欢:下载图标和使用静态文件,或者使用操作系统注册的图标。

所以我继续研究并研究了 VS 扩展 FileIcons. What this extension does is it replaces VS Icons with its own. And the Monikers.imagemanifest and IconIsMoniker caught my attention. Searching for that I ran into the Image service and Moniker documentation. This explains in details what is Moniker, how VS uses the Images, etc. The breakthrough was IVsImageService2 and there is a code example how to use KnownMonikers. A bit adjusting that example I was able to query and retry Icons registered in Visual Studio with a very simple code you can check it on Github

//Acquire the IVsImageService2in the Package
var imageService = (IVsImageService2)Package.GetGlobalService(typeof(SVsImageService));

//Using the IVsImageService2 the query icons.
public Icon GetIcon(IClosedDocument document)
{
    IVsUIObject uIObj = _vsImageService2.GetIconForFile(document.Name, __VSUIDATAFORMAT.VSDF_WINFORMS);
    Icon icon = (Icon)GelUtilities.GetObjectData(uIObj);

    return icon;
}