共用方式為


Windows2.CreateToolWindow2 方法

建立新的工具視窗,並在其中裝載使用者定義的 .NET 控制項。

命名空間:  EnvDTE80
組件:  EnvDTE80 (在 EnvDTE80.dll 中)

語法

'宣告
Function CreateToolWindow2 ( _
    Addin As AddIn, _
    Assembly As String, _
    Class As String, _
    Caption As String, _
    GuidPosition As String, _
    <OutAttribute> ByRef ControlObject As Object _
) As Window
Window CreateToolWindow2(
    AddIn Addin,
    string Assembly,
    string Class,
    string Caption,
    string GuidPosition,
    out Object ControlObject
)
Window^ CreateToolWindow2(
    AddIn^ Addin, 
    String^ Assembly, 
    String^ Class, 
    String^ Caption, 
    String^ GuidPosition, 
    [InAttribute] [OutAttribute] Object^% ControlObject
)
abstract CreateToolWindow2 : 
        Addin:AddIn * 
        Assembly:string * 
        Class:string * 
        Caption:string * 
        GuidPosition:string * 
        ControlObject:Object byref -> Window 
function CreateToolWindow2(
    Addin : AddIn, 
    Assembly : String, 
    Class : String, 
    Caption : String, 
    GuidPosition : String, 
    ControlObject : Object
) : Window

參數

  • Addin
    型別:EnvDTE.AddIn
    建立工具視窗的增益集執行個體。
  • Assembly
    型別:System.String
    包含使用者控制項之組件的完整名稱或檔案路徑。
  • Class
    型別:System.String
    實作使用者控制項之類別的完整名稱。
  • Caption
    型別:System.String
    要顯示在新工具視窗中的標題。
  • GuidPosition
    型別:System.String
    新視窗的唯一識別項 (它可用以在 Windows 集合中尋找此視窗)。
  • ControlObject
    型別:System.Object%
    要在新工具視窗中裝載的使用者控制項。

傳回值

型別:EnvDTE.Window
Window 物件。

備註

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

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

如需這個方法的使用範例,請參閱 Visual Studio Automation 範例網頁上的 ToolWindow 範例:https://www.microsoft.com/downloads/details.aspx?familyid=3ff9c915-30e5-430e-95b3-621dccd25150&displaylang=en。 如需建立 ActiveX 控制項的相關資訊,請參閱建立 MFC ActiveX 控制項

範例

您必須先利用建置 Windows 控制項程式庫專案的方式建立使用者控制項之後,才能進行下列範例。 請留意要在下列程式碼中使用之控制項的專案和類別名稱。 必須將 assemblypath 字串變更為使用者控制項之 DLL 檔所在的目錄, 另外,這個程式碼是設計用來取代增益集專案的 OnConnection 方法。

[C#]

public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    EnvDTE80.Windows2 wins2obj;
    AddIn addinobj;
    object ctlobj = null;
    Window newWinobj;

    // A toolwindow must be connected to an add-in, so this line 
    // references one.
    addinobj = _applicationObject.AddIns.Item(1);
    wins2obj = (Windows2)_applicationObject.Windows;

    // This section specifies the path and class name of the windows 
    // control that you want to host in the new tool window, as well as 
    // its caption and a unique GUID.
    string assemblypath = "C:\\temp\\WindowsControlLibrary1.dll";
    string classname = "WindowsControlLibrary1.UserControl1";
    string guidpos = "{426E8D27-3D33-4FC8-B3E9-9883AADC679F}";
    string caption = "CreateToolWindow2 Test";

    // Create the new tool window and insert the user control in it.
    newWinobj = wins2obj.CreateToolWindow2(addinobj, assemblypath, 
      classname, caption, guidpos, ref ctlobj);
    newWinobj.Visible = true;
}

.NET Framework 安全性

請參閱

參考

Windows2 介面

EnvDTE80 命名空間

其他資源

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

HOW TO:編譯和執行 Automation 物件模型程式碼範例