visual-studio 在 Visual Studio 中折叠所有项目的最佳/最快/最简单的方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/83009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:31:24  来源:igfitidea点击:

What's the best/fastest/easiest way to collapse all projects in Visual Studio?

visual-studio

提问by Tom Robinson

I'm currently using DPackas this adds a "Collapse All Projects" option to the Solution node in Solution Explorer. It works pretty well but can take a while to execute and doesn't always collapse everything fully.

我目前正在使用DPack,因为这会向解决方案资源管理器中的解决方案节点添加“折叠所有项目”选项。它工作得很好,但可能需要一段时间才能执行并且并不总是完全折叠所有内容。

Are there any better alternatives? Preferably free and easy to install/setup. There are lots out there but which work best and don't have any bugs or performance issues.

有没有更好的选择?最好免费且易于安装/设置。那里有很多,但效果最好并且没有任何错误或性能问题。

采纳答案by Andy Stevenson

For VS2005, I've been using CoolCommands 4.0. The feature description is more complete for the older 3.0 version. Version 3 had an .msi installer. Version 4 is a .zip file(which was easier for my environment anyway).

对于 VS2005,我一直在使用CoolCommands 4.0。对于较旧的 3.0 版本,功能描述更为完整。版本 3 有一个 .msi 安装程序。版本 4 是一个.zip 文件(无论如何这对我的环境来说更容易)。

My favorite features (a subset of the complete list):

我最喜欢的功能(完整列表的一个子集):

  • From the Solution explorer:
    • Collapse All Projects
    • Open containing folder (Project/file level only)
  • From the filename tabs above the editor
    • Locate in Solution Explorer
  • From the context menu in the editor
    • Demo Font
  • 从解决方案资源管理器:
    • 折叠所有项目
    • 打开包含文件夹(仅限项目/文件级别)
  • 从编辑器上方的文件名选项卡
    • 在解决方案资源管理器中定位
  • 从编辑器的上下文菜单中
    • 演示字体

回答by Tom Robinson

I use the following Macro which works in Visual Studio 2005 and Visual Studio 2008:

我使用以下适用于 Visual Studio 2005 和 Visual Studio 2008 的宏:

  1. View > Other Windows > Macro Explorer (Alt+F8)
  2. Right-click on the MyMacros node in Macro Explorer
  3. New Module...
  4. Name it CollapseAll (or whatever you like)
  5. Replace the default code with the code shown below
  6. File > Save CollapseAll (Ctrl+S)
  7. Close the Macro editor
  1. 视图 > 其他窗口 > 宏资源管理器 (Alt+F8)
  2. 右键单击宏资源管理器中的 MyMacros 节点
  3. 新模块...
  4. 将其命名为 CollapseAll(或您喜欢的任何名称)
  5. 用下面显示的代码替换默认代码
  6. 文件 > 保存全部折叠 (Ctrl+S)
  7. 关闭宏编辑器

To set up a keyboard shortcut:

要设置键盘快捷键:

  1. Tools > Customize... > Commands
  2. Keyboard...
  3. Show commands containing: Macros.MyMacros.CollapseAll.CollapseAll
  4. Assign a keyboard shortcut (I use Alt+C)
  1. 工具 > 自定义... > 命令
  2. 键盘...
  3. 显示命令包含:Macros.MyMacros.CollapseAll.CollapseAll
  4. 分配键盘快捷键(我使用 Alt+C)

Code

代码

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module CollapseAll
    Sub CollapseAll()

        ' Get the the Solution Explorer tree
        Dim solutionExplorer As UIHierarchy
        solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

        ' Check if there is any open solution
        If (solutionExplorer.UIHierarchyItems.Count = 0) Then
            Return
        End If

        ' Get the top node (the name of the solution)
        Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)

        rootNode.DTE.SuppressUI = True

        ' Collapse each project node
        Collapse(rootNode, solutionExplorer)

        ' 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.

        rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
        rootNode.DTE.SuppressUI = False

    End Sub

    Sub CollapseSelected()

        ' Get the the Solution Explorer tree
        Dim solutionExplorer As UIHierarchy
        solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()

        ' Check if there is any open solution
        If (solutionExplorer.UIHierarchyItems.Count = 0) Then
            Return
        End If

        ' Get the top node (the name of the solution)
        Dim selected As Array = solutionExplorer.SelectedItems

        If (selected.Length = 0) Then Return

        Dim rootNode As UIHierarchyItem = selected(0)
        rootNode.DTE.SuppressUI = True

        ' Collapse each project node
        Collapse(rootNode, solutionExplorer)

        ' 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.

        rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
        rootNode.DTE.SuppressUI = False

    End Sub

    Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)

        For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
            If innerItem.UIHierarchyItems.Count > 0 Then

                ' Re-cursive call
                Collapse(innerItem, solutionExplorer)

                ' Collapse
                If innerItem.UIHierarchyItems.Expanded Then
                    innerItem.UIHierarchyItems.Expanded = False
                    If innerItem.UIHierarchyItems.Expanded = True Then
                        ' Bug in VS 2005
                        innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
                        solutionExplorer.DoDefaultAction()
                    End If
                End If

            End If
        Next

    End Sub

End Module

I didn't write this code and I'm not sure where this code came from, but there are variations of it online.

这段代码不是我写的,我不确定这段代码是从哪里来的,但网上有它的变体。

回答by aku

Power commands for visual studiowill do the trick. Didn't notice any performance\stability issues with them.

Visual Studio 的电源命令可以解决问题。没有注意到它们有任何性能\稳定性问题。

回答by Mario

Here is a better list of features for CoolCommands 4.0.

这是CoolCommands 4.0的更好功能列表。

To install it for VS 2005, execute the include setup.bat.

要为 VS 2005 安装它,请执行 include setup.bat。

To install it for VS 2008, modify the following line from

要为 VS 2008 安装它,请修改以下行

regpkg CoolCommands.dll /codebase

to:

到:

regpkg CoolCommands.dll /root:Software\Microsoft\VisualStudio.0 /codebase

回答by Dan Esparza

PowerCommands for Visual Studiowill work for both VS2008 and VS2010. It is the Microsoft-enabled way to do this quickly.

PowerCommands for Visual Studio将适用于 VS2008 和 VS2010。这是 Microsoft 支持的快速执行此操作的方法。