< Free Open Study > 

The Windows Collection

The Windows collection differs from the Documents collection in that the Documents collection contains only editable windows, while the Windows collection contains all windows in the environment (IDE), including tool windows, such as the Output window, Command window, Solution window, Immediate window, and so forth. You can do special things in the Windows collection that you are not able to do in the Documents collection, such as dock windows together from your code in the add-in.

Manipulating Docked Windows

It might not be likely that you'll use the facility described in this section. However, I'll demonstrate docking some of the tool windows in the IDE, just to illustrate that you can control just about everything in the IDE from your add-in. The code sample in Listing 5-15 will frame and dock three dockable tool windows together, change the size of one of the windows, and finally undock a window from the frame. Because it's fairly unlikely that you would use this particular code in an add-in, I'm going to use a feature of the IDE that I haven't yet introduced: the Macro Explorer. You'll investigate the Macro Explorer in Chapter 8, but for now, if you have never used it, this will be a simple exercise.

Listing 5-15: Docking Tool Windows Example

start example
     Sub DockingExample()         Dim WinFrame As Window         Dim Win1 As Window         Dim Win2 As Window         Dim Win3 As Window         ' Create three tool windows in the IDE         Win1 = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer)         Win2 = DTE.Windows.Item(Constants.vsWindowKindToolbox)         Win3 = DTE.Windows.Item(Constants.vsWindowKindCommandWindow)         'Create a linked window frame and dock the Solution Explorer         ' and Toolbox windows together inside it.         WinFrame = DTE.Windows.CreateLinkedWindowFrame(Win1, Win2, _                                vsLinkedWindowType.vsLinkedWindowTypeDocked)         MsgBox("Total number of windows in the linked window frame: " & _                         WinFrame.LinkedWindows.Count)         ' Add the Command window         ' to the frame with the other two.         WinFrame.LinkedWindows.Add(Win3)         MsgBox("Total number of windows in the linked window frame: " & _                         WinFrame.LinkedWindows.Count)         'Resize the entire linked window frame.         WinFrame.Width = 400         WinFrame.Height = 700         MsgBox("Frame height and width changed. Now " & _               "changing Command window height.")         'Resize the Command window.         WinFrame.LinkedWindows.Item(3).Height = 600         MsgBox("Now undocking the Command window from the frame.")         ' Undock the Command window from the frame.         WinFrame.LinkedWindows.Remove(Win3)     End Sub 
end example

First, open any project in Visual Studio. The type of project does not matter, as this demo will not alter the project. This demo will only manipulate tool windows, but there must be a project open in order for the tool windows to be visible.

Next, if the Macro Explorer window is not visible, select Tools Macros Macro Explorer. You can also open the Macro Explorer by pressing Alt-F8. In the window, you should see a TreeView with a node named My Macros. If there are no modules under My Macros, right-click the My Macros node and click the New Module menu option. If there is a module (child node) there already, you can open the Macros IDE by right-clicking the module and clicking the Edit menu option.

When the Macros IDE opens, it will look much like the regular IDE. Copy the code in its entirety from Listing 5-15 and paste it into the macro module that you opened in the Macros IDE.

Next, switch back to the regular IDE, and in the Macro Explorer window right-click the DockingExample TreeView node. When the pop-up menu appears, click Run and watch the tool windows as they are manipulated. Message boxes will describe what's happening. That's all there is to it!


Because this code is being executed in the Macros IDE rather than in an add-in, I have changed the normal application object name oVB to DTE.You must always reference the development environment in the Macros IDE by using the DTE object.

Clearing the Command Window

Let's take time for one more simple exercise related to the tool windows in the IDE. This time you will open a new instance of the Output window, which is where the results of Debug.Writeline messages appear. You will place some text into the window and then display a message box so that you can see the text in the Output window. Finally, the code will clear the Output window, which, by the way, might be a feature that you'll want to add to your add-in. I'm sure that by now you are able to do that without my help if you choose.

Again, you will use the macro facility to demonstrate this functionality. Follow the steps described in the previous section and place the code from Listing 5-16 into the Macros IDE. In the regular IDE, right-click OutputWindowExample and choose the Run option.

Listing 5-16: Output Window Example

start example
     Sub OutputWindowExample()         ' Get a reference to the Command window.         Dim win As Window = _             DTE.Windows.Item(EnvDTE.Constants.vsWindowKindCommandWindow)         Dim CW As CommandWindow = win.Object         ' Insert some information text into the Command window.         CW.OutputString("This text will be displayed in the output window")         ' Clear the contents of the Command window.         MsgBox("Click Ok to clear the Command window...")         CW.Clear()     End Sub 
end example

 < Free Open Study > 

Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith © 2008-2017.
If you may any questions please contact us: