How to: Open a Dynamic Tool Window
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.
注意
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.
注意
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(GetType(DynamicWindowPane), PositionX:=250, PositionY:=250, Width:=160, Height:=180, Transient:=True), _ MsVsShell.ProvideToolWindowVisibility(GetType(DynamicWindowPane), "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 Inherits MsVsShell.Package
[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. Dim pane As MsVsShell.ToolWindowPane = Me.FindToolWindow(GetType(DynamicWindowPane), 0, True) If pane Is Nothing Then
// 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.
Dim frame As IVsWindowFrame = TryCast(pane.Frame, IVsWindowFrame) If frame Is Nothing Then
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())
// Bring the tool window to the front and give it focus ErrorHandler.ThrowOnFailure(frame.Show());