我在vs2005和vs2008中找到了适用于我的CollapseAll宏。但是,这种方式在vs2010中有效。看起来它只折叠顶级节点而不是任何可以扩展的子节点?任何想法?
谢谢, 杆
Sub CollapseAll()
' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
' MsgBox("Nothing to collapse. You must have an open solution.")
Return
End If
' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
UIHSolutionRootNode.DTE.SuppressUI = True
' Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
'UIHItem.UIHierarchyItems.Expanded = False
If UIHItem.UIHierarchyItems.Expanded Then
Collapse(UIHItem)
End If
Next
' Select the solution node, or else when you click
' on the solution window
' scrollbar, it will synchronize the open document
' with the tree and pop
' out the corresponding node which is probably not what you want.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
UIHSolutionRootNode.DTE.SuppressUI = False
End Sub
Private Sub Collapse(ByVal item As UIHierarchyItem)
For Each eitem As UIHierarchyItem In item.UIHierarchyItems
If eitem.UIHierarchyItems.Expanded AndAlso eitem.UIHierarchyItems.Count > 0 Then
Collapse(eitem)
End If
Next
item.UIHierarchyItems.Expanded = False
End Sub
End Module
答案 0 :(得分:8)
(是的,我知道你将另一个答案标记为已被接受,但从技术上来说,这并没有回答你的编码问题,尽管它确实给你一个答案。但这是你最初提出的问题。)
实际上,我认为这不适用于任何版本的Visual Studio,因为Collapse功能中的逻辑是错误的。与项目中的初始循环相同(我不确定为什么你只是没有将解决方案节点传递给崩溃函数...)
具体来说,您在钻研孩子之前检查它是否已展开,因此如果文件夹已折叠但其子项已展开,则它将不会进入并折叠子项,因为您的检查会导致跳过这些子项。只需查看计数。毕竟你想要深入研究一切崩溃。
这是我用你的作为起点创建的版本。我还添加了重新选择所选项目的能力(如果有的话)。我喜欢我正在选择的项目。如果我希望所有内容都崩溃,我要么选择根节点,要么取消选择所有内容,在这两种情况下它都会选择根(解决方案节点)。
我还添加了MultiBeep功能以发出声音反馈,因为我不喜欢消息框。 2声嘟嘟声成功,3表示您没有打开解决方案。
Sub CollapseSolutionTree()
' Get the the Solution Explorer tree
Dim SolutionExplorer As UIHierarchy = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (SolutionExplorer.UIHierarchyItems.Count = 0) Then
MultiBeep(3)
Return
End If
' Get the selected node (if any)
Dim SelectedNode As UIHierarchyItem
Try
SelectedNode = SolutionExplorer.SelectedItems(0)
Catch
End Try
' Get the top node (the name of the solution)
Dim SolutionNode As UIHierarchyItem = SolutionExplorer.UIHierarchyItems.Item(1)
SolutionNode.DTE.SuppressUI = True
' Collapse the solution tree
CollapseSolutionExplorerNode(SolutionNode)
' If there was a selected item before the command, re-select it. Otherwise select the solution node.
If SelectedNode isnot Nothing
SelectedNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
Else
SolutionNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
End If
SolutionNode.DTE.SuppressUI = False
MultiBeep(2)
End Sub
Private Sub CollapseSolutionExplorerNode(ByVal Folder As UIHierarchyItem)
For Each Subfolder As UIHierarchyItem In Folder.UIHierarchyItems
If Subfolder.UIHierarchyItems.Count Then CollapseSolutionExplorerNode(Subfolder)
Next
Folder.UIHierarchyItems.Expanded = False
End Sub
Private Sub MultiBeep(Count As Integer, Optional Spacing As Integer = 100)
Beep
For I = 2 to Count
System.Threading.Thread.CurrentThread.Sleep(Spacing)
Beep
Next
End Sub
HTH,
标记
答案 1 :(得分:2)
在Sara Ford的博客上有一个VS2010扩展名:
答案 2 :(得分:0)
此功能是Visual Studio 11的免费Microsoft Productivity Power Tools扩展的一部分,也可能在下一版本的Visual Studio中作为标准功能找到。