代码之家  ›  专栏  ›  技术社区  ›  Tara McGrew

在VS 2017自定义项目系统中,如何在解决方案浏览器中使项目项加粗?

  •  5
  • Tara McGrew  · 技术社区  · 7 年前

    我正在为VS 2017编写一个项目系统扩展,用我的语言编写的每个项目都有一个文件,即“启动文件”。我希望该文件在解决方案资源管理器中显示为粗体。

    Python Tools for VS 做我想要的,但我的扩展是建立在新的项目系统框架(CPS)上的。CPS更改解决方案资源管理器项外观的方法是实现 IProjectTreePropertiesProvider

    1 回复  |  直到 7 年前
        1
  •  5
  •   Simon Mourier    7 年前

    [Export(typeof(IProjectTreePropertiesProvider))]
    [AppliesTo(MyUnconfiguredProject.UniqueCapability)]
    [Order(1000)]
    internal class ProjectTreePropertiesProvider1 : IProjectTreePropertiesProvider
    {
        // we need to import that to do COM calls
        [Import]
        protected IProjectThreadingService ThreadingService { get; set; }
    
        // we want the "old" IVsHierarchy interface 
        [ImportMany(ExportContractNames.VsTypes.IVsHierarchy)]
        private OrderPrecedenceImportCollection<IVsHierarchy> IVsHierarchies { get; }
        private IVsHierarchy VsHierarchy => IVsHierarchies.First().Value;
    
        [ImportingConstructor]
        public ProjectTreePropertiesProvider1(UnconfiguredProject unconfiguredProject)
        {
            IVsHierarchies = new OrderPrecedenceImportCollection<IVsHierarchy>(projectCapabilityCheckProvider: unconfiguredProject);
        }
    
        /// <summary>
        /// Calculates new property values for each node in the project tree.
        /// </summary>
        /// <param name="propertyContext">Context information that can be used for the calculation.</param>
        /// <param name="propertyValues">Values calculated so far for the current node by lower priority tree properties providers.</param>
        public async void CalculatePropertyValues(IProjectTreeCustomizablePropertyContext propertyContext, IProjectTreeCustomizablePropertyValues propertyValues)
        {
            // this is from the standard WindowsScript project type sample
            if (propertyValues.Flags.Contains(ProjectTreeFlags.Common.ProjectRoot))
            {
                // etc..
                propertyValues.Icon = KnownMonikers.JSProjectNode.ToProjectSystemType();
                // etc..
            }
    
            // now, we're doing some COM calls, ensure it happens on UI thread
            await ThreadingService.SwitchToUIThread();
    
            // get the id of some item (this "Start.js" item is from the standard sample)
            VsHierarchy.ParseCanonicalName("Start.js", out uint id);
    
            // get IVsUIShell from service provider
            VsHierarchy.GetSite(out Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp);
            var shell = (IVsUIShell)sp.QueryService<IVsUIShell>();
    
            // get solution explorer's window
            var SolutionExplorer = new Guid(ToolWindowGuids80.SolutionExplorer);
            shell.FindToolWindow(0, ref SolutionExplorer, out IVsWindowFrame frame);
    
            // get solution explorer's DocView
            frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out object obj);
            var window = (IVsUIHierarchyWindow2)obj;
    
            // change attribute of the item
            window.SetItemAttribute((IVsUIHierarchy)VsHierarchy, id, (uint)__VSHIERITEMATTRIBUTE.VSHIERITEMATTRIBUTE_Bold, true);
        }
    }