宏观崩溃全部在解决方案visual studio 2010中

Rod*_*Rod 4 visual-studio-2010 visual-studio

我在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
Run Code Online (Sandbox Code Playgroud)

Mar*_*eIV 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
Run Code Online (Sandbox Code Playgroud)

HTH,

标记