I tried to use IVsHierarchy.AdviseHierarchyEvents with VSHPROPID_Expanded to IVsSolution , but the event is not fire.
So next, I tried registering event detect from tree control.
// PresentationCore
// PresentationFramework
// System.Xaml
// WindowsBase
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Collections.Generic;
using System.Linq;
using static Microsoft.VisualStudio.ErrorHandler;
namespace Gekka.VisualStudio.SolutionExploreExpandHackPackage
{
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.EmptySolution, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasSingleProject, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasMultipleProjects, PackageAutoLoadFlags.BackgroundLoad)]
[Guid(SolutionExploreExpandHackPackage.PackageGuidString)]
public sealed class SolutionExploreExpandHackPackage : AsyncPackage
{
public const string PackageGuidString = "026bc0c8-e89a-412e-aae1-66a5f34f7727";
protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
System.Windows.EventManager.RegisterClassHandler
(typeof(Microsoft.Internal.VisualStudio.PlatformUI.PivotTreeView)
, Microsoft.Internal.VisualStudio.PlatformUI.PivotTreeView.PreviewMouseDownEvent
, new System.Windows.RoutedEventHandler((s, e) =>
{
if (s.GetType().Name.Contains("SolutionPivotTreeView"))
{
var ptv = (Microsoft.Internal.VisualStudio.PlatformUI.PivotTreeView)s;
ptv.NodeExpanded -= treeView_NodeExpanded;
ptv.NodeExpanded += treeView_NodeExpanded;
}
}), true);
}
private void treeView_NodeExpanded(object sender, Microsoft.Internal.VisualStudio.PlatformUI.TreeNodeEventArgs e)
{
if (e.Node != null)
{
Dump(e.Node, 0);
}
}
private static void Dump(Microsoft.Internal.VisualStudio.PlatformUI.IVirtualizingTreeNode node, int lv)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (node.Item is Microsoft.VisualStudio.Shell.IAttachedCollectionSource cs)
{
if (cs.SourceItem is Microsoft.VisualStudio.Shell.IVsHierarchyItem hitem)
{
IVsHierarchyItemIdentity hii = hitem.HierarchyIdentity;
Succeeded(hii.Hierarchy.GetProperty(hii.ItemID, (int)__VSHPROPID.VSHPROPID_Name, out var name));
Succeeded(hii.Hierarchy.GetProperty(hii.ItemID, (int)__VSHPROPID.VSHPROPID_Caption, out var caption));
Succeeded(hii.Hierarchy.GetProperty(hii.ItemID, (int)__VSHPROPID2.VSHPROPID_IsLinkFile, out var isLink));
System.Diagnostics.Debug.WriteLine($"{new string(' ', lv)}└ {caption}\t{name}\t{isLink}");
}
}
foreach (Microsoft.Internal.VisualStudio.PlatformUI.IVirtualizingTreeNode childNode in node.ChildNodes)
{
Dump(childNode, lv + 1);
}
}
}
}
Solution Explore can be displayed in multiple views, this code can detect expand the nodes in each view.