[英]In a VS 2017 custom project system, how can I make a project item bold in the Solution Explorer?
我正在編寫VS 2017的項目系統擴展,並且用我的語言編寫的每個項目都有一個文件,即“啟動文件”。 我希望該文件在解決方案資源管理器中顯示為粗體。
VS的Python工具可以滿足我的需求,但是我的擴展是基於新的項目系統框架(CPS)構建的。 更改解決方案資源管理器項外觀的CPS方法是實現IProjectTreePropertiesProvider
,但是我看不到有任何方法來更改文本樣式-只是圖標。
我不確定CPS是否為此內置了任何功能,但是您仍然可以混合使用“舊的”本機/托管Visual Studio界面。 這是一個使用IProjectTreePropertiesProvider的示例:
[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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.