Working with Microsoft Visual Studio 2005

The Output window is where Visual Studio displays text information generated by tools such as compilers or the debugger. The Output window is also a perfect place for any tools you create that generate text information that might be useful to the user. In fact, throughout this book, the sample macros and add-ins use the class library OutputWindowPaneEx to display text in the Output window as these samples do their work.

The object behind the Output window is called OutputWindow, and you can find this object by using code such as this:

Sub FindOutputWindow() Dim window As EnvDTE.Window Dim outputWindow As EnvDTE.OutputWindow window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) outputWindow = CType(window.Object, EnvDTE.OutputWindow) End Sub

Output Window Panes

The user interface of the Output window consists of a number of view ports, or panes, each of which displays text. You can switch between these panes by selecting a pane by name from the drop-down list at the top of the Output window. You can enumerate the panes by using the OutputWindowPanes object, as shown here:

Sub EnumOutputWindowPanes() Dim window As EnvDTE.Window Dim outputWindow As EnvDTE.OutputWindow Dim outputWindowPanes As EnvDTE.OutputWindowPanes Dim outputWindowPane As EnvDTE.OutputWindowPane 'Find the OutputWindow object window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) outputWindow = CType(window.Object, EnvDTE.OutputWindow) 'Retrieve the OutputWindowPanes object outputWindowPanes = outputWindow.OutputWindowPanes 'Enumerate each OutputWindowPane For Each outputWindowPane In outputWindowPanes MsgBox(outputWindowPane.Name) Next End Sub

You can also use the OutputWindowPanes object to create new panes. The method Add takes as its only argument the name of the new pane to create:

Sub CreateOutputWindowPane() Dim window As EnvDTE.Window Dim outputWindow As EnvDTE.OutputWindow Dim outputWindowPanes As EnvDTE.OutputWindowPanes Dim outputWindowPane As EnvDTE.OutputWindowPane 'Find the OutputWindow object window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) outputWindow = CType(window.Object, EnvDTE.OutputWindow) 'Retrieve the OutputWindowPanes object outputWindowPanes = outputWindow.OutputWindowPanes 'Add a new pane: outputWindowPane = outputWindowPanes.Add("My New Pane") End Sub

This macro creates a new output window pane named My New Pane that's ready to be filled with the text output of your add-in or macro code. You can inject text into this window by using the OutputWindowPane.OutputString method, which takes a string that's appended to the end of other text in the appropriate pane. As strings are placed into the Output window pane, they're injected without a line break between them; this means that if a new line character needs to be placed between each string, you must write the code to do this. The following macro sample displays the contents of the folder containing the solution file that's currently open; as each file path is displayed in the Output window pane, a line break (or ASCII value 13) is inserted:

Sub DisplaySolutionDirectory() Dim files As String() Dim file As String Dim directoryOutputWindowPane As OutputWindowPane Dim fullName As String Dim outputWindow As OutputWindow outputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object 'Find the folder the solution is in, as well as the files that are ' in that folder: fullName = System.IO.Path.GetDirectoryName(DTE.Solution.FullName) files = System.IO.Directory.GetFiles(fullName) 'Try to find a "Solution Directory" pane, if one does not exist, ' create it: With outputWindow.OutputWindowPanes Try directoryOutputWindowPane = .Item("Solution Directory") 'Show the pane: directoryOutputWindowPane.Activate() Catch directoryOutputWindowPane = .Add("Solution Directory") End Try End With 'Clear the pane: directoryOutputWindowPane.Clear() For Each file In files 'Display the file path, with a line break between each line directoryOutputWindowPane.OutputString(file + Chr(13)) Next End Sub

This macro demonstrates the use of a few methods and properties of the OutputWindowPane object. The Activate method makes sure the pane corresponding to the instance of the OutputWindowPane that it's being called on is the same pane displayed to the user; it simulates the selection of that pane from the drop-down list in the Output window. OutputString dumps a string into the pane, and Clear removes all text from that pane. Another property, TextDocument, which isn't shown in this macro, deserves special note. It returns an EnvDTE.TextDocument object for the pane that's read-only—you can retrieve the contents of this window, but not change it. (You can use OutputString only to modify the contents.) We'll discuss this object in further detail in the next chapter.

Категории