How to: Open a Dynamic Tool Window (C#)
Tool windows are typically opened from a command on a menu, or an equivalent keyboard shortcut. At times, however, you might need a tool window that opens whenever a particular user interface (UI) context applies, and closes when the UI context no longer applies. Tool windows like these are called dynamic or auto-visible.
Note |
|---|
| For a list of predefined UI contexts, see VSConstants and look for fields that begin with UICONTEXT_. |
Use FindToolWindow to open a tool window. The tool window is created as needed.
Note |
|---|
| A dynamic tool window can be closed by the user. If you want to create a menu command so the user can reopen the tool window, the menu command should be enabled in the same UI context that opens the tool window, and disabled otherwise. |
The code in the following procedure is taken from the Reference.ToolWindow Sample (C#).
To open a dynamic tool window
-
Create the tool window pane, frame, and the VSPackage that implements them. For more information, see How to: Create a Tool Window (C#).
-
Register the tool window with Visual Studio by adding the ProvideToolWindowAttribute and ProvideToolWindowVisibilityAttribute to the VSPackage that provides it.
[MsVsShell.ProvideToolWindow(typeof(DynamicWindowPane), PositionX=250, PositionY=250, Width=160, Height=180, Transient=true)] [MsVsShell.ProvideToolWindowVisibility(typeof(DynamicWindowPane), /*UICONTEXT_SolutionExists*/"f1536ef8-92ec-443c-9ed7-fdadf150da82")] [MsVsShell.ProvideMenuResource(1000, 1)] [MsVsShell.DefaultRegistryRoot(@"Software\Microsoft\VisualStudio\8.0Exp")] [MsVsShell.PackageRegistration(UseManagedResourcesOnly = true)] [Guid("01069CDD-95CE-4620-AC21-DDFF6C57F012")] public class PackageToolWindow : MsVsShell.Package {This registers the tool window DynamicWindowPane as transient, that is, not persisted when Visual Studio is closed and reopened. DynamicWindowPane is opened whenever UICONTEXT_SolutionExists applies, and closed otherwise. A default location and size is specified. For more information, see How to: Register a Tool Window (C#).
-
Use FindToolWindow to find the tool window pane or to create it if it does not already exist.
// Get the 1 (index 0) and only instance of our tool window. // If it does not already exist it will get created. MsVsShell.ToolWindowPane pane = this.FindToolWindow(typeof(DynamicWindowPane), 0, true); if (pane == null) { -
Get the tool window frame from the tool window pane.
IVsWindowFrame frame = pane.Frame as IVsWindowFrame; if (frame == null) { -
Show the tool window.
// Bring the tool window to the front and give it focus ErrorHandler.ThrowOnFailure(frame.Show());
Note