在Visual Studio 2010中,宏折叠全部解决方案

4
我在网上找到了一个名为CollapseAll的宏,它在vs2005和vs2008中对我有用。然而,在vs2010中,它只能折叠顶级节点,而不能折叠任何已展开的子节点。你有什么想法吗?
谢谢, rod.
    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

1
能否请您重新考虑一下您接受的答案?您投票支持的那个答案可能解决了您特定的问题,但实际上并没有回答您的问题。而我提供的答案则确实回答了您的问题。将其标记为最佳答案有助于我的声望,毕竟,声望是该网站的基础。谢谢! :) - Mark A. Donohoe
3个回答

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,

Mark


1
同意你的观点。(是的,我知道你已经将另一个答案标记为已接受,但从技术上讲,那并没有回答你的编码问题,尽管它确实给了你一个答案。而这个回答则是你最初提出的问题的答案。)谢谢,这帮了我大忙。我不想添加一个插件来使我的VS进一步变慢。 - IsmailS
谢谢。很遗憾他们没有这么做。声望是该网站的基础,而那个答案并没有真正回答编程问题,只是展示了另一种达到期望结果的方式。我的问题是这对想自己编写代码的其他人没有帮助,因此我给出了我的答案。 - Mark A. Donohoe


0

此功能是 Visual Studio 11 的免费 Microsoft Productivity Power Tools 扩展的一部分,很可能也会作为标准功能出现在下一个版本的 Visual Studio 中。


它不是生产力工具扩展的一部分,而是Power Commands的一部分。 - IsmailS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接