Export (0) Print
Expand All

Windows2.CreateToolWindow2 Method 

Creates a new tool window and hosts a user-defined, .NET control in it.

Namespace: EnvDTE80
Assembly: EnvDTE80 (in envdte80.dll)

Window CreateToolWindow2 (
	AddIn Addin,
	string Assembly,
	string Class,
	string Caption,
	string GuidPosition,
	[InAttribute] out Object ControlObject
)
Window CreateToolWindow2 (
	AddIn Addin, 
	String Assembly, 
	String Class, 
	String Caption, 
	String GuidPosition, 
	/** @attribute InAttribute() */ /** @attribute OutAttribute() */ /** @ref */ Object ControlObject
)
JScript does not support passing value-type arguments by reference.

Parameters

Addin

An instance of the Add-in creating the tool window.

Assembly

The full name or file path of the assembly containing the User control.

Class

The full name of the class implementing the User control.

Caption

The caption to display in the new tool window.

GuidPosition

A unique identifier for the new window. (This can be used to locate the window in the Windows collection.)

ControlObject

The User control to be hosted in the new tool window.

Return Value

A Window object.

Before invoking CreateToolWindow2 to create a new tool window, you should either move the User control (ControlObject) into the same assembly as the add-in, or set all of the attributes on the User control to make it fully visible to COM. (For example, checking the Register for COM interop option in the project's compile options.) If you do not do this, then the control will not marshal correctly and CreateToolWindow2 will return a null value.

If you attempt to set visibility states of the new tool window — such as height, width, or position — before the tool window is visible, you get an error. Make sure that the window is visible before attempting to set any such properties.

For more examples of how to use this method, see the ToolWindow sample on the Visual Studio Automation Samples Web page: http://www.microsoft.com/downloads/details.aspx?familyid=3ff9c915-30e5-430e-95b3-621dccd25150&displaylang=en. For information about creating ActiveX controls, see Creating an MFC ActiveX Control.

The following example requires that you first create a User control by building a Windows Control Library project. Note the name of the control's project and class for use in the following code. Change the assemblypath string to the directory of the User control's DLL file. Also, the code is designed to replace the OnConnection method of an add-in project.

[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;
}
Show:
© 2014 Microsoft