Windows2.CreateToolWindow2 方法

更新:2007 年 11 月

创建新工具窗口并在其中承载用户定义的 .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
用法
Dim instance As Windows2
Dim Addin As AddIn
Dim Assembly As String
Dim Class As String
Dim Caption As String
Dim GuidPosition As String
Dim ControlObject As Object
Dim returnValue As Window

returnValue = instance.CreateToolWindow2(Addin, _
    Assembly, Class, Caption, GuidPosition, _
    ControlObject)
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
)
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 互操作注册”选项。)如果您不执行此操作,则控件不会正确封送,而且 CreateToolWindow2 将返回一个空值。

如果您尝试在新工具窗口可见之前设置新工具窗口的可见性状态(如高度、宽度或位置),则会得到错误。确保在尝试设置任何这样的属性之前窗口是可见的。

有关如何使用此方法的更多示例,请参见“Visual Studio Automation Samples”(vsprvs 自动化示例)网页上的 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;
}

权限

另请参见

参考

Windows2 接口

Windows2 成员

EnvDTE80 命名空间

其他资源

如何:创建和控制工具窗口

如何:编译和运行自动化对象模型代码示例