Introduction
When we open up a Visual Studio solution which contains ample projects, usually all the projects will be in expanded mode. There is no direct tool menu supported in the Visual Studio IDE to collapse all the expanded projects. So I thought of creating a Visual Studio plug-in which serves the purpose. On rummaging around, I could find so many macros that support the functionality that I mentioned but I was unable to find a pure C# code that can be used to implement the Collapse All functionality in .NET. So I thought of creating one of my own using C#.
The below mentioned are the various steps for developing the Add-in.
Figure 1 - Creating a Visual Studio Add-in Project
Once you have created a Visual Studio Add-in Project, create a class which is extended from IDTExtensibility2 and IDTCommandTarget. The main objects that you have to use are the DTE2 and AddIn. You need to add reference binaries in order to include EnvDTE and EnvDTE80.
I have used the below two functions to implement the core Collapse all functionality.
</span> /// Command to Collapse All Nodes.
/// <span class="code-SummaryComment"></summary>
</span> private void DoCollapse()
/// <span class="code-SummaryComment"><summary>
</span> /// To Collapse all the nodes in the tree
/// <span class="code-SummaryComment"></summary>
</span> /// <span class="code-SummaryComment"><param name="item"></param>
</span> /// <span class="code-SummaryComment"><param name="solutionExplorer"></param>
</span> private void Collapse(UIHierarchyItem item,ref UIHierarchy solutionExplorer)
The DoCollapse function is called from the Exec function of IDTCommandTarget interface.
</span>/// This is called when the command is invoked.<span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param term='commandName'>The name of the command to execute.</param>
</span>/// <span class="code-SummaryComment"><param term='executeOption'>Describes how the command should be run.</param>
</span>/// <span class="code-SummaryComment"><param term='varIn'>Parameters passed from the caller to the command handler.</param>
</span>/// <span class="code-SummaryComment"><param term='varOut'>Parameters passed from the command handler to the caller.</param>
</span>/// <span class="code-SummaryComment"><param term='handled'>Informs the caller if the command was handled or not.</param>
</span>/// <span class="code-SummaryComment"><seealso class='Exec' />
</span>public void Exec(string commandName, vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
Add Name to your Tool bar Menu
You can add your own name in the tool bar by changing the argument in the AddNamedCommand2. In the stated example, I have given the name of the tool bar as “CollapseAll_SNK“.
Add an Icon to the Tool bar Menu
In void OnConnection() function in commands.AddNamedCommand2() function, I have used 6743 as the 5th Argument for function AddNamedCommand2. Have you noticed the Red Star in the CollaspeAll_SNK menu, 6743 is the index value for the red star. The default index value is 59 the icon is a smiley-face. To change to a different standard icon, change this index number with range 59 to 6743.
If you cannot find an appropriate icon in the Microsoft.VisualStudio.CommandBars library, you can use a custom bitmap for the add-in's command icon. The bitmap is contained as a resource in a satellite DLL. For more information, see How to: Display a Custom Icon on the Add-in Button. After creating the satellite DLL resource, you then point AddNamedCommand2 to the custom icon.
Figure 2 - Collapse All Tool Menu in VS2010
Figure 3 - Collapse All Tool Menu in VS2008 and VS2005
About Collapse All Add in Installer
I have also created an Add Installer for the above created Add-in. Using this installer, you can install the Collapse All add-in to your machine by selecting which Visual Studio you need to have the add-in attached. After installation, you should close your Visual Studio if it was already open to reflect the changes.
Figure 4 - Collapse All Add-in Installer UI
Included Binaries
- CollapseAll.dll
- CollapseAll_SNK.AddIn
- CollaspeAll_AddinInstaller.exe
Conclusion
Hope this article will give you a general idea on how to go about creating a Visual Studio Add-in and more over the primary thing is to get the created Collapse All Add-in for your Visual Studio.
References