
Introduction
This article describes how to use the Extensibility Framework of Visual Studio 2005/2008 to create tool windows for add-ins that integrate perfectly into the IDE's docking environment.
Background
It is useful to be able to create a plug-in for VS 2005/2008 and to know some of the classes in the Extensibility library.
Using the code
In this example, I will create an integration for the Windows Media Player into Visual Studio. No matter what the benefits of this integration could be, I want to show how to create tool windows that fit the Visual Studio look and integrate into the floating/docking-window environment. I stumbled over this problem when I first tried to create a VS plug-in. I discovered it was really easy to let Visual Studio show some dialogs or do some other stuff, but I wasn't able to create dialogs that fit into the IDE's normal look. I wanted to create tool windows that were floating on the IDE's GUI and that could be docked to the left, right, top, bottom, center in tab pages, horizontally and vertically, and so on.
So, I will start at the point where the Visual Studio add-in is created using the AddIn wizard. If you want to create a dialog that integrates into VS, you can not do it by creating a Windows.Form, but by creating a UserControl.
In our example, we want Visual Studio to host a Media Player object. So, we a UserControl called MediaPlayer and drag the Windows Media Player control to the surface using the designer. The Windows Media Player COM control can be added to the Toolbox using the wizard for adding controls to the Toolbox. Since this control is a COM component, you will have to select it on the COM Tabpage of the wizard. Now, we add a ToolStrip to the top of the MediaPlayer to be able to send some commands to the Windows Media Player.
That iss all for our simple user control. Now, the work continues in the Connect class that is created by the AddIn Wizard.
We will now write the code necessary to create a tool window that hosts our control and to open it. The right place for us to write this code is the Exec method of the Connect class which will be called when the add-in is executed.
private Window2 win = null;
This member will contain the tool window.
public void Exec(string commandName, vsCommandExecOption executeOption,
ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "VSMediaPlayer2.Connect.VSMediaPlayer2")
{
if (this.win == null)
this.CreateToolWindow();
this.win.Visible = true;
handled = true;
return;
}
}
}
This method is called when the add-in is executed. Depending on the settings, this can be at the startup or on clicking in the main menu entry of the add-in. If the member variable is initialized once, it doesn't need to be created again, so we will simply set the window's Visible property to true, to let it show up.
private void CreateToolWindow()
{
string guid = "{87907470-2841-11de-8c30-0800200c9a66}";
object obj = new object();
Windows2 windows = (Windows2)this._applicationObject.Windows;
string asm = Assembly.GetCallingAssembly().Location;
this.win = (Window2)windows.CreateToolWindow2(this._addInInstance, asm,
"VSMediaPlayer2.MediaPlayer", "MediaPlayer", guid, ref obj);
}
The actual creation of the tool window and the assignment of our control takes place in this method. The main work is done by the library method CreateToolWindow2. For the use of this method, you need following:
- the
AddInInstance object which is part of the Connect class
- the executing assembly
- the full identifier of the control that needs to be hosted
- the name for the head of your tool window
- a GUID
- a status object
That is it. Now, Visual Studio creates a native VS tool window that floats or docks on the IDE's surface just as the other VS tools like the Toolbox or the Class-Browser.
The result is shown in the picture in the top section of this article.
Additional
I will keep you posted on other Visual Studio Extension projects with downloads and tutorials here, and on my German website: http://www.eigene-software.de.