Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

Toggle Tool Windows in VS.NET

Rate me:
Please Sign up or sign in to vote.
1.60/5 (2 votes)
19 Mar 20032 min read 41.6K   294   9   4
De/Activate common tool windows without using the mouse

Introduction

Note: This is an enhancement of an article written by Kevin McFarlane. The original article can be found here.

When working in the .Net IDE, it's very easy to become overwhelmed by the numerous new tool windows. The macro presented here will automatically de/activate the tool windows. When the macro is called to deactivate the tool windows, it will note which tool windows are currently active before deactivating only those windows. Subsequently, only those windows that were deactivated will be re-activated. In this way, the same keyboard shortcut can be used to toggle between activating & deactivating the tool windows.

Background

As great as the .Net IDE is, having all the tool windows open can distract the programmer from focusing on the code. For example:

Sample screenshot

Here, it’s very hard to read the code, because of all the tool windows that are open. This macro will hide all the active tool windows:

Sample screenshot

so, now it’s much easier to concentrate on the code. The next invocation of the macro will restore all the tool windows.

Using the code

To use the macro, assign it a shortcut key. I assigned “Alt-T” to this macro, because it’s not preassigned by the IDE. Every time you press the shortcut key the tool windows will be de/activated.

Points of Interest

To check if a tool window is open, the macro calls CanToggleToolWindow:

VB.NET
Function CanToggleToolWindow(ByVal vsWindowKind As String, _
                             ByRef ToolWin As Window) As Boolean
    Try
        ToolWin = DTE.Windows.Item(vsWindowKind)
    Catch ex As System.IndexOutOfRangeException
        Return False
    Catch ex As System.Exception
        MsgBox(ex.GetType().ToString() & vbNewLine & vbNewLine & ex.Message)
        Return False
    End Try
    Return True
End Function

DTE.Windows.Item(vsWindowKind) throws an exception if the index is out of range. If an exception is thrown we assume that the desired tool window is not open. Therefore, we will not de/activate that tool window. Otherwise, a reference to the window is returned.

To toggle a single tool window, use

VB.NET
Sub ToggleToolWindow(ByVal vsWindowKind As String, ByRef ShouldToggle As Boolean)
    Dim ToolWin As Window

    If (Not CanToggleToolWindow(vsWindowKind, ToolWin)) Then
        ShouldToggle = False
        Exit Sub
    End If

    If (ToolWindowsAreVisible) Then
        If (ToolWin.Visible()) Then
            ShouldToggle = True
        End If
        ToolWin.Visible = False
    Else
        If (ShouldToggle) Then
            ToolWin.Visible = True
        End If
    End If
End Sub

The ShouldToggle parameter indicates whether the macro deactivated this window the last time. The variable ToolWindowsAreVisible is a toggle which controls de/activation.

Using the above two routines, the macro to toggle the tool windows is coded as:

VB.NET
Sub Toggle_Tool_Windows()

    '////////////////////////////////////////////
    'DESCRIPTION: Closes/Opens tool windows
    '////////////////////////////////////////////

    ToggleToolWindow(Constants.vsWindowKindClassView, ClassViewWasOpen)
    ToggleToolWindow(Constants.vsWindowKindCommandWindow, CommandWindowWasOpen)
    ToggleToolWindow(Constants.vsWindowKindMacroExplorer, MacroExplorerWasOpen)
    ToggleToolWindow(Constants.vsWindowKindObjectBrowser, ObjectBrowserWasOpen)
    ToggleToolWindow(Constants.vsWindowKindOutput, OutputWasOpen)
    ToggleToolWindow(Constants.vsWindowKindProperties, PropertiesWasOpen)
    ToggleToolWindow(Constants.vsWindowKindSolutionExplorer, SolutionExplorerWasOpen)
    ToggleToolWindow(Constants.vsWindowKindTaskList, TaskListWasOpen)
    ToggleToolWindow(Constants.vsWindowKindToolbox, ToolboxWasOpen)
    ToolWindowsAreVisible = (Not ToolWindowsAreVisible)

End Sub

Finally, the toggles that determine the state of the tool windows are defined as:

VB.NET
Dim ToolWindowsAreVisible As Boolean = True
Dim ClassViewWasOpen As Boolean
Dim CommandWindowWasOpen As Boolean
Dim MacroExplorerWasOpen As Boolean
Dim ObjectBrowserWasOpen As Boolean
Dim OutputWasOpen As Boolean
Dim PropertiesWasOpen As Boolean
Dim SolutionExplorerWasOpen As Boolean
Dim TaskListWasOpen As Boolean
Dim ToolboxWasOpen As Boolean

You can define additional tool windows by defining a toggle for the window, and calling ToggleToolWindow in Toggle_Tool_Windows.

Hope you find this article helpful.

History

<st1:date month="3" day="9" year="2003">3/9/2003 Original version 1.0 by <st1:personname>Behnam Nikbakht

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAnother Approach Pin
Ronzo31-Mar-03 11:56
Ronzo31-Mar-03 11:56 
GeneralRe: Another Approach Pin
Patrick Spieler3-Sep-03 0:22
Patrick Spieler3-Sep-03 0:22 
GeneralRe: Another Approach Pin
Member 1093030610-Oct-05 5:16
Member 1093030610-Oct-05 5:16 
Or how about:

<br />
DTE.Windows.Item(Constants.vsWindowKind).Visible = Not DTE.Windows.Item(Constants.vsWindowKind).Visible <br />


Rinse, repeat Wink | ;)
GeneralSwitch to full screen Pin
Thomas Freudenberg21-Mar-03 1:14
Thomas Freudenberg21-Mar-03 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.