Bagikan melalui


Output Window (Visual Studio SDK)

The Output window is a set of text panes that you can write to and read from. Visual Studio defines these built-in panes: Build, through which projects communicate messages about builds, and General, through which Visual Studio communicates messages about the integrated development environment (IDE). Projects receive a reference to the Build pane automatically through the IVsBuildableProjectCfg interface methods, and Visual Studio offers direct access to the General pane through the SVsGeneralOutputWindowPane service. In addition to the built-in panes, you can create and manage your own custom panes.

You can control the Output window directly through the IVsOutputWindow and IVsOutputWindowPane interfaces. The IVsOutputWindow interface, which is offered by the SVsOutputWindow service, defines methods for creating, retrieving, and destroying Output window panes. The IVsOutputWindow interface defines methods for showing panes, hiding panes, and manipulating their text. An alternative way of controlling the Output window is through the OutputWindow and OutputWindowPane objects in the Visual Studio Automation object model. These objects encapsulate nearly all of the functionality of the IVsOutputWindow and IVsOutputWindowPane interfaces. In addition, the OutputWindow and OutputWindowPane objects add some higher-level functionality to make it easier to enumerate the Output window panes and to retrieve text from the panes.

Example

Description

This example shows how to create a new Output window pane by using the IVsOutputWindow interface.

Code

Private Function CreatePane(ByVal paneGuid As Guid, ByVal title As String, ByVal visible As Boolean, ByVal clearWithSolution As Boolean) As IVsOutputWindowPane 
    Dim output As IVsOutputWindow = DirectCast(GetService(GetType(SVsOutputWindow)), IVsOutputWindow) 
    Dim pane As IVsOutputWindowPane 
    
    ' Create a new pane. 
    output.CreatePane(paneGuid, title, Convert.ToInt32(visible), Convert.ToInt32(clearWithSolution)) 
    
    ' Retrieve the new pane. 
    output.GetPane(paneGuid, pane) 
    
    Return pane 
End Function
IVsOutputWindowPane CreatePane(Guid paneGuid, string title, 
    bool visible, bool clearWithSolution)
{
    IVsOutputWindow output = 
        (IVsOutputWindow)GetService(typeof(SVsOutputWindow));
    IVsOutputWindowPane pane;

    // Create a new pane.
    output.CreatePane(
        ref paneGuid, 
        title, 
        Convert.ToInt32(visible), 
        Convert.ToInt32(clearWithSolution));
    
    // Retrieve the new pane.
    output.GetPane(ref paneGuid, out pane);

    return pane;
}

Example

Description

This example shows how to create an Output window pane by using the OutputWindow object.

Code

Private Function CreatePane(ByVal title As String) As OutputWindowPane 
    Dim dte As DTE2 = DirectCast(GetService(GetType(DTE)), DTE2) 
    Dim panes As OutputWindowPanes = dte.ToolWindows.OutputWindow.OutputWindowPanes 
    
    Try 
        ' If the pane exists already, return it. 
        Return panes.Item(title) 
    Catch generatedExceptionName As ArgumentException 
        ' Create a new pane. 
        Return panes.Add(title) 
    End Try 
End Function
OutputWindowPane CreatePane(string title)
{
    DTE2 dte = (DTE2)GetService(typeof(DTE));
    OutputWindowPanes panes =
        dte.ToolWindows.OutputWindow.OutputWindowPanes;

    try
    {
        // If the pane exists already, return it.
        return panes.Item(title);
    }
    catch (ArgumentException)
    {
        // Create a new pane.
        return panes.Add(title);
    }
}

Comments

Although the OutputWindowPanes collection lets you retrieve an Output window pane by its title, pane titles are not guaranteed to be unique. When you doubt the uniqueness of a title, use the GetPane method to retrieve the correct pane by its GUID.

Example

Description

This example shows how to delete an Output window pane.

Code

Private Sub DeletePane(ByVal paneGuid As Guid) 
    Dim output As IVsOutputWindow = DirectCast(GetService(GetType(SVsOutputWindow)), IVsOutputWindow) 
    
    output.DeletePane(paneGuid) 
End Sub
void DeletePane(Guid paneGuid)
{
    IVsOutputWindow output =
        (IVsOutputWindow)GetService(typeof(SVsOutputWindow));

    output.DeletePane(ref paneGuid);
}

Example

Description

This example shows how to delete an Output window pane, given an OutputWindowPane object.

Code

Private Sub DeletePane(ByVal pane As OutputWindowPane) 
    Dim output As IVsOutputWindow = DirectCast(GetService(GetType(SVsOutputWindow)), IVsOutputWindow) 
    Dim paneGuid As New Guid(pane.Guid) 
    
    output.DeletePane(paneGuid) 
End Sub
void DeletePane(OutputWindowPane pane)
{
    IVsOutputWindow output =
        (IVsOutputWindow)GetService(typeof(SVsOutputWindow));
    Guid paneGuid = new Guid(pane.Guid);

    output.DeletePane(ref paneGuid);
}

Example

Description

This example shows how to retrieve the built-in General pane of the Output window.

Code

Private Function GetGeneralPane() As IVsOutputWindowPane 
    Return DirectCast(GetService(GetType(SVsGeneralOutputWindowPane)), IVsOutputWindowPane) 
End Function
IVsOutputWindowPane GetGeneralPane()
{
    return (IVsOutputWindowPane)GetService(
        typeof(SVsGeneralOutputWindowPane));
}

Example

Description

This example shows how to parse a standard build message for errors, and add an item to the Error window, if appropriate, before the message is sent to the Output window.

Code

Private Sub OutputTaskItemStringExExample(ByVal buildMessage As String, ByVal buildPane As IVsOutputWindowPane, ByVal launchPad As IVsLaunchPad) 
    Dim priority As UInteger() = New UInteger(0) {}, lineNumber As UInteger() = New UInteger(0) {} 
    Dim fileName As String() = New String(0) {}, taskItemText As String() = New String(0) {} 
    Dim taskItemFound As Integer() = New Integer(0) {} 
    
    ' Determine whether buildMessage contains an error. 
    launchPad.ParseOutputStringForTaskItem(buildMessage, priority, fileName, lineNumber, taskItemText, taskItemFound) 
    
    
    ' If buildMessage contains an error, send it to both the 
    ' Error window and the Output window; otherwise, send it 
    ' to the Output window only. 
    If taskItemFound(0) <> 0 Then 
        buildPane.OutputTaskItemStringEx(buildMessage, DirectCast(priority(0), VSTASKPRIORITY), VSTASKCATEGORY.CAT_BUILDCOMPILE, Nothing, 0, fileName(0), _ 
        lineNumber(0), taskItemText(0), Nothing) 
    Else 
        buildPane.OutputString(buildMessage) 
    End If 
    
    buildPane.OutputString(vbLf) 
End Sub
void OutputTaskItemStringExExample(string buildMessage,
    IVsOutputWindowPane buildPane, IVsLaunchPad launchPad)
{
    uint[] priority = new uint[1], lineNumber = new uint[1];
    string[] fileName = new string[1], taskItemText = new string[1];
    int[] taskItemFound = new int[1];

    // Determine whether buildMessage contains an error.
    launchPad.ParseOutputStringForTaskItem(
        buildMessage, 
        priority, 
        fileName, 
        lineNumber, 
        taskItemText, 
        taskItemFound);


    // If buildMessage contains an error, send it to both the 
    // Error window and the Output window; otherwise, send it
    // to the Output window only.
    if (taskItemFound[0] != 0)
    {
        buildPane.OutputTaskItemStringEx(
            buildMessage, 
            (VSTASKPRIORITY)priority[0], 
            VSTASKCATEGORY.CAT_BUILDCOMPILE, 
            null, 
            0, 
            fileName[0], 
            lineNumber[0], 
            taskItemText[0], 
            null);
    }
    else
    {
        buildPane.OutputString(buildMessage);
    }

    buildPane.OutputString("\n");
}

See Also

Reference

IVsLaunchPad

IVsLaunchPadFactory

IVsOutputWindow

IVsOutputWindowPane

OutputWindow

OutputWindowPane

SVsGeneralOutputWindowPane

SVsLaunchPadFactory

SVsOutputWindow