Share via


HOW TO:建立並控制工具視窗

Visual Studio 中的視窗可分為兩類:文件視窗或工具視窗。 文件視窗的內容可以用程式碼編輯器進行編輯,例如文字檔、HTML 檔或類別內部的程式碼。 工具視窗會包含一個或多個控制項,例如按鈕、文字、下拉式方塊等。 Visual Studio 整合式開發環境 (IDE) 會使用控制項執行某些工作,例如設定選項、檢視錯誤,或是編輯專案項目。 像是 [輸出] 視窗、[工作清單] 和 [工具箱] 等,這些都是控制項。 [工具箱] 可自由地在 IDE 內部移動或停駐於其他工具視窗,而且您可以使用 LinkedWindows 集合,以程式設計方式連結或取消連結 IDE 中的工具視窗。 如需詳細資訊,請參閱HOW TO:變更視窗特性

除了使用 Automation 操作現有的工具視窗外,您也可以使用 Windows2 集合的 CreateToolWindow2 方法建立自己的自訂工具視窗。

建立自己的自訂工具視窗後,就可以填入各種可用來執行工作的控制項。 例如,您可以使用自訂工具視窗顯示特製的工具,協助您格式化程式碼、追蹤和變更變數設定,或是執行進階偵錯工作或來源分析。

建立自訂工具視窗的程序如下:

  • 建立使用者控制項 (User Control) (使用 Windows 控制項程式庫專案)。

  • 在表單上加入您想要的控制項 (例如按鈕、文字方塊等) 和程式碼。

  • 將專案編譯為 DLL。

  • 建立新的 Visual Studio 增益集專案 (或是其他專案,例如 Windows 應用程式專案)。

  • 使用 CreateToolWindow2 方法來建立工具視窗,以便裝載 (Host) 新的使用者控制項。

叫用 CreateToolWindow2 來建立新的工具視窗之前,您應該將使用者控制項 (ControlObject) 與增益集移入相同的組件,或設定使用者控制項的所有屬性以便讓 COM 完全可見此控制項 (例如,在專案的編譯選項中,核取 [註冊 COM Interop] 選項)。如果您沒有這樣做,此控制項就無法正確封送處理,而且 CreateToolWindow2 會傳回 null 值。

除了下面的範例外,Visual Studio Automation 範例 (英文) 網站上還提供了其他各種語言的工具視窗範例以及其他程式碼範例。

注意事項注意事項

如果您嘗試在可以看見工具視窗前,設定新工具視窗的任何可視狀態 (例如高度、寬度或位置),將會收到錯誤訊息。 請在嘗試設定這類屬性之前,確認視窗是可見的。

注意事項注意事項

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

建立自訂工具視窗

下列範例會示範如何在 Visual Basic 和 Visual C# 中建立工具視窗。

注意事項注意事項

下列程式碼必須在增益集中執行,不能在巨集中執行。

若要建立自訂工具視窗

  • 請在 Windows 控制項程式庫專案中建立使用者控制項。 您可以直接採用 "WindowsControlLibrary1" 這個預設名稱,或是在下面的程式碼中變更 asmPath 參數的名稱,但是必須確定要符合 Windows 控制項程式庫專案的名稱。

    或者,您也可以在程式碼中參考現有的使用者控制項。

注意事項注意事項

您的使用者控制項類別必須具有附加至類別定義的 System.Runtime.InteropServices.GuidAttribute

  1. 建立新的增益集專案。

    如需詳細資訊,請參閱 HOW TO:建立增益集

  2. 以下列程式碼取代增益集的 OnConnection 方法:

    Public Sub OnConnection(ByVal application As Object, ByVal _
    connectMode As ext_ConnectMode, ByVal addInInst As Object, _
    ByRef custom As Array) Implements IDTExtensibility2.OnConnection
        Try
            ' ctlProgID - the ProgID for your user control.
            ' asmPath - the path to your user control DLL.
            ' guidStr - a unique GUID for the user control.
            Dim ctlProgID, asmPath, guidStr As String
            ' Variables for the new tool window that will hold
            ' your user control.
            Dim toolWins As EnvDTE80.Windows2
            Dim toolWin As EnvDTE.Window
            Dim objTemp As Object = Nothing
    
            _applicationObject = CType(application, DTE2)
            _addInInstance = CType(addInInst, AddIn)
            ctlProgID = "WindowsControlLibrary2.UserControl1"
            ' Replace the <Path to VS Project> with the path to
            ' the folder where you created the WindowsCotrolLibrary.
            ' Remove the line returns from the path before 
            ' running the add-in.
            asmPath = "<Path to VS Project>\My _
              Documents\Visual Studio 2005\Projects\ _
              WindowsControlLibrary2\WindowsControlLibrary2\_
              bin\Debug\WindowsControlLibrary2.dll"
            guidStr = "{E9C60F2B-F01B-4e3e-A551-C09C62E5F584}"
    
            toolWins = CType(_applicationObject.Windows, Windows2)
            ' Create the new tool window, adding your user control.
            toolWin = toolWins.CreateToolWindow2(_addInInstance, _
              asmPath, ctlProgID, "MyNewToolwindow", guidStr, objTemp)
            ' The tool window must be visible before you do anything 
            ' with it, or you will get an error.
            If Not toolWins Is Nothing Then
                toolWin.Visible = True
            End If
               ' Uncomment the code below to set the new tool window's
               ' height and width, and to close it.
            ' MsgBox("Setting the height to 500 and width to 400...")
            ' toolWin.Height = 500
            ' toolWin.Width = 400
            ' MsgBox("Closing the tool window...")
            ' toolWin.Close(vsSaveChanges.vsSaveChangesNo)
    
        Catch ex As Exception
            MsgBox("Exception: " & ex.ToString)
        End Try
    End Sub
    
    // Before running, add a reference to System.Windows.Forms, 
    // using System.Windows.Forms, to the top of the class.
    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        try
        {
            // ctlProgID - the ProgID for your user control.
            // asmPath - the path to your user control DLL.
            // guidStr - a unique GUID for the user control.
            string ctlProgID, asmPath, guidStr;
            // Variables for the new tool window that will hold
            // your user control.
            EnvDTE80.Windows2 toolWins;
            EnvDTE.Window toolWin;
            object objTemp = null;
    
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;
            ctlProgID = "WindowsControlLibrary2.UserControl1";
            // Replace the <Path to VS Project> with the path to
            // the folder where you created the WindowsCotrolLibrary.
            // Remove the line returns from the path before 
            // running the add-in.
            asmPath = @"c:\My Documents\Visual Studio 2005\Projects\
              WindowsControlLibrary2\WindowsControlLibrary2\bin\
              Debug\WindowsControlLibrary2.dll";
            guidStr = "{E9C60F2B-F01B-4e3e-A551-C09C62E5F584}";
    
            toolWins = (Windows2)_applicationObject.Windows;
            // Create the new tool window, adding your user control.
            toolWin = toolWins.CreateToolWindow2(_addInInstance, 
              asmPath, ctlProgID, "MyNewToolwindow", guidStr, 
              ref objTemp);
            // The tool window must be visible before you do anything 
            // with it, or you will get an error.
            if (toolWins != null)
            {
                toolWin.Visible = true;
            }
            // Set the new tool window's height and width, 
            // and then close it.
            System.Windows.Forms.MessageBox.Show("Setting the height 
            to 500 and width to 400...");
            toolWin.Height = 500;
            toolWin.Width = 400;
            System.Windows.Forms.MessageBox.Show
              ("Closing the tool window...");
            toolWin.Close(vsSaveChanges.vsSaveChangesNo);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Exception: " 
              + ex.Message);
        }
    }
    

    **注意:**上述程式碼需要 System.Windows.Forms 命名空間的參考。

  3. 變更 ctlProgID、asmPath 和 guidStr 變數的值以反映您的使用者控制項。

  4. 建置及執行專案。

  5. 在 [工具] 功能表中,按一下 [增益集管理員] 啟動增益集。

您便會看見新的工具視窗浮現在 IDE 中。 您可以將它移動到任何位置,或是將它停駐在其他工具視窗旁。

請參閱

工作

HOW TO:變更視窗特性

HOW TO:建立增益集

逐步解說:建立精靈

概念

控制選項設定

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考