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

C# 如何从C代码中获取Visual Studio文件类型图标作为位图,c#,wpf,visual-studio,visual-studio-extensions,envdte,C#,Wpf,Visual Studio,Visual Studio Extensions,Envdte,目前我正在研究一种类似于Web浏览器的方法。使用热键Ctrl+Shift+T重新打开上一个关闭的“选项卡” 我还添加了一个工具窗口。用户可以在其中查看完整的已关闭文档历史记录。我想在此WPF窗口中添加的是文件类型的实际图标,而不是在Windows中注册到这些扩展名的图标。但是解决方案资源管理器窗格中显示的实际图标。我想让它们以C代码的形式显示为Icon/Bitmap/byte[]任何内容。因此,我可以将其转换为WPF BitmapSource,并将其动态绑定到ListView。所以我不想从互联网

目前我正在研究一种类似于Web浏览器的方法。使用热键Ctrl+Shift+T重新打开上一个关闭的“选项卡”

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


DTE2对象的接口有点复杂,很难找到任何东西。到目前为止,我还没有找到任何解决办法。你知道我该怎么做吗?

你可以使用Windows API阅读

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

这是我的版本:(主要针对.NET4.7更新)

使用系统;
使用System.Collections.Generic;
使用系统图;
使用System.IO;
使用System.Runtime.InteropServices;
使用System.Windows;
使用System.Windows.Interop;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
名称空间掠夺
{
公共静态类IconManager
{
私有静态只读字典smallIconCache=新字典();
私有静态只读字典LargeIncache=new Dictionary();
公共静态图像源FindIconForFilename(字符串文件名,bool isLarge)
{
var extension=Path.GetExtension(文件名);
if(extension==null)返回null;
var cache=isLarge?largeincache:smallIconCache;
if(cache.TryGetValue(扩展,输出变量图标))返回图标;
图标=图标阅读器
.GetFileIcon(文件名,isLarge?IconReader.IconSize.Large:IconReader.IconSize.Small,false)
.ToImageSource();
cache.Add(扩展名、图标);
返回图标;
}
专用静态图像源到图像源(此图标)
{
var成像源=
CreateBitmapSourceFromHIcon(icon.Handle,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
返回图像源;
}
私有静态类IconReader
{
公共枚举图标化
{
大=0,
小=1
}
公共静态图标GetFileIcon(字符串名称、图标大小、布尔链接覆盖)
{
var shfi=new Shell32.Shfileinfo();
var flags=Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;
if(linkOverlay)flags+=Shell32.ShgfiLinkoverlay;
如果(IconSize.Small==大小)
flags+=Shell32.ShgfiSmallicon;
其他的
flags+=Shell32.ShgfiLargeicon;
Shell32.SHGetFileInfo(名称,
Shell32.0异常,
高飞,
(uint)SizeOf元帅(shfi),
旗帜);
var icon=(icon)icon.FromHandle(shfi.hIcon.Clone();
User32.DestroyIcon(shfi.hIcon);
返回图标;
}
}
私有静态类Shell32
{
private const int MaxPath=256;
[StructLayout(LayoutKind.Sequential)]
公共结构Shfileinfo
{
私有常量int Namesize=80;
公共只读IntPtr hIcon;
专用只读int iIcon;
私有只读uint属性;
[Marshallas(UnmanagedType.ByValTStr,SizeConst=MaxPath)]私有只读字符串szDisplayName;
[Marshallas(UnmanagedType.ByValTStr,SizeConst=Namesize)]私有只读字符串szTypeName;
}
公安ShgfiIcon=0x000000100;
公共警察ShgfiLinkoverlay=0x00008000;
公共警察ShgfiLargeicon=0x000000000;
公共警察ShgfiSmallicon=0x000000001;
公共警察ShgfiUsefileattributes=0x000000010;
public consuint FileAttributeNormal=0x00000080;
[DllImport(“Shell32.dll”)]
公共静态外部IntPtr SHGetFileInfo(
字符串pszPath,
uint dwFileAttributes,
参考Shfileinfo psfi,
uint cbFileInfo,
uint uFlags
);
}
私有静态类User32
{
[DllImport(“User32.dll”)]
公共静态外部int销毁图标(IntPtr hIcon);
}
}
}
我用这段代码做了一个开源程序,你可以在这里找到它:这是一个文件搜索程序。显示基于此代码搜索的每个文件的图标


谢谢你的回答和评论。不幸的是,他们都提出了我在问题中提到的一些建议,我不想这样做。比如:下载图标并使用静态文件,或者使用操作系统注册的图标

所以我继续我的研究,并研究了VS扩展。这个扩展的作用是用自己的图标替换VS图标。还有名字.imagemanifest图标名字引起了我的注意。我在文档中找到了这个。这详细解释了什么是名字、VS如何使用图像等。突破点是IVsImageService2,还有一个如何使用KnownMonikers的代码示例。稍微调整一下这个例子,我就可以用一个非常简单的代码查询并重试在VisualStudio中注册的图标,您可以查看它


如果静态资源正常,那么MSFT最近会发布它们的图标-@Bijan。是的,我知道。但正如我在问题中所写,我不想
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);
        }
    }
}
//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;
}