Share via


HOW TO:控制輸出視窗

[輸出] 視窗會顯示整合式開發環境 (IDE) 中各種功能的狀態訊息, 其中包括在編譯專案時發生的建置錯誤,以及根據目標資料庫檢查預存程序中的 Transact-SQL 語法時的結果。 某些特定的 IDE 功能 (例如在 [命令] 視窗中叫用的外部工具功能或命令) 會傳送輸出至特殊的 [輸出] 視窗窗格。 通常顯示在 Windows 命令提示字元視窗中的外部工具 (例如 .bat 檔或 .com 檔) 輸出,也可以顯示在 [輸出] 視窗中。

Visual Studio Automation 模型提供了下列可用來控制 [輸出] 視窗的物件:

物件名稱

描述

OutputWindow

表示 [輸出] 視窗。

OutputWindowPanes

包含所有 [輸出] 視窗窗格的集合。

OutputWindowPane

表示 [輸出] 視窗中的單一窗格。

OutputWindowEvents

可讓您回應 [輸出] 視窗中發生的事件。

除了控制 [輸出] 視窗的內容外,您也可以控制視窗的特性,例如寬度和高度。 如需詳細資訊,請參閱HOW TO:變更視窗特性

[輸出] 視窗窗格內的文字,可以使用 Visual Studio 編輯器 Automation 模型中的 TextDocument 物件、EditPoint 物件和其他類似物件來操作,就像是在 [程式碼編輯器] 中編輯程式碼一樣。 如需詳細資訊,請參閱HOW TO:控制程式碼編輯器 (Visual Basic)

注意事項注意事項

根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。 使用 [一般開發設定] 開發了這些程序。 如果要變更設定,請按一下 [工具] 功能表上的 [匯入匯出設定]。 如需詳細資訊,請參閱 使用設定

範例

在這個範例中,會示範如何在 [輸出] 視窗中加入新的視窗窗格,以及如何在窗格中加入一些文字。 如需如何執行範例的詳細資訊,請參閱 HOW TO:編譯和執行 Automation 物件模型程式碼範例

Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, _
  ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    ' Pass the applicationObject member variable to the code example.
    OutputWindowTest(_applicationObject)
End Sub

Sub OutputWindowTest(ByVal dte As DTE2)
    ' Create a tool window reference for the Output window
    ' and window pane.
    Dim ow As OutputWindow = dte.ToolWindows.OutputWindow
    Dim owP As OutputWindowPane

    ' Add a new pane to the Output window.
    owP = ow.OutputWindowPanes.Add("A New Pane")
    ' Add a line of text to the new pane.
    owP.OutputString("Some Text")
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    OutputWindowTest(_applicationObject);
}

public void OutputWindowTest(DTE2 dte)
{
    // Create a tool window reference for the Output window
    // and window pane.
    OutputWindow ow = dte.ToolWindows.OutputWindow;
    OutputWindowPane owP;

    // Add a new pane to the Output window.
    owP = ow.OutputWindowPanes.Add("A New Pane");
    // Add a line of text to the new pane.
    owP.OutputString("Some Text");
}

在這個範例中,會在 [輸出] 視窗的 [建置] 窗格中加入文字,然後擷取文字。

Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, _
  ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    ' Pass the applicationObject member variable to the code example.
    writeReadOW(_applicationObject)
End Sub

Sub writeReadOW(ByVal dte As DTE2)
    ' Add-in code.
    ' Create a reference to the Output window.
    ' Create a tool window reference for the Output window
    ' and window pane.
    Dim ow As OutputWindow = dte.ToolWindows.OutputWindow
    Dim owP As OutputWindowPane
    ' Create a reference to the pane contents.
    Dim owPTxtDoc As TextDocument

    ' Select the Build pane in the Output window.
    owP = ow.OutputWindowPanes.Item("Build")
    owP.Activate()
    owPTxtDoc = owP.TextDocument

    ' Put some text in the pane.
    owP.OutputString("Testing 123.")
    ' Retrieve the text contents of the pane.
    MsgBox("Startpoint: " & owPTxtDoc.StartPoint.DisplayColumn)
    MsgBox(owPTxtDoc.StartPoint.CreateEditPoint. _
      GetText(owPTxtDoc.EndPoint))
End Sub
using System.Windows.Forms;
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    writeReadOW(_applicationObject);
}

public void writeReadOW(DTE2 dte)
{
    // Add-in code.
    // Create a reference to the Output window.
    // Create a tool window reference for the Output window
    // and window pane.
    OutputWindow ow = dte.ToolWindows.OutputWindow;
    OutputWindowPane owP;
    // Create a reference to the pane contents.
    TextDocument owPTxtDoc;
    EditPoint2 strtPt;

    // Select the Build pane in the Output window.
    owP = ow.OutputWindowPanes.Item("Build");
    owP.Activate();
    owPTxtDoc = owP.TextDocument;
            
    // Put some text in the pane.
    owP.OutputString("Testing 123.");
    // Retrieve the text contents of the pane.
    System.Windows.Forms.MessageBox.Show("Startpoint: " + 
      owPTxtDoc.StartPoint.DisplayColumn);
    strtPt = (EditPoint2)owPTxtDoc.StartPoint.CreateEditPoint();
    System.Windows.Forms.MessageBox.Show
      (strtPt.GetText(owPTxtDoc.EndPoint));
}

請參閱

工作

HOW TO:變更視窗特性

HOW TO:建立增益集

逐步解說:建立精靈

概念

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考