如何:打开工具窗口以编程方式

按等效的键盘快捷键通常打开工具窗口中单击菜单中的命令,或。 但是,您可能必须打开一个工具窗口以编程方式,如命令处理程序。

若要打开了它的托管 VSPackage 的工具窗口,请使用 FindToolWindow。 若要打开另一 VSPackage 的工具窗口,请使用 FindToolWindow。 在任何情况下,工具窗口。创建 (根据要求。

下面的代码从 C# Reference.ToolWindow 示例中采用。

打开工具窗口以编程方式

  1. 创建工具窗口窗格、框架和实现自己的 VSPackage。 有关更多信息,请参见 如何:创建一个工具窗口

  2. 注册了 Visual Studio 的工具窗口通过添加 ProvideToolWindowAttribute 到提供它的 VSPackage。

    <PackageRegistration(UseManagedResourcesOnly:=True), _
    InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400), _
    ProvideMenuResource(1000, 1), _
    ProvideToolWindow(GetType(MyToolWindow), Style:=VsDockStyle.Tabbed, Window:="3ae79031-e1bc-11d0-8f78-00a0c9110057"), _
    DefaultRegistryRoot("Software\Microsoft\VisualStudio\8.0Exp"), _
    Guid("01069CDD-95CE-4620-AC21-DDFF6C57F012")> _
    Public NotInheritable Class PackageToolWindow
        Inherits Package
    
    [ProvideToolWindow(typeof(MyToolWindow), Style = VsDockStyle.Tabbed, Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057")]
    [ProvideMenuResource(1000, 1)]
    [DefaultRegistryRoot(@"Software\Microsoft\VisualStudio\8.0Exp")]
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [Guid("01069CDD-95CE-4620-AC21-DDFF6C57F012")]
    public sealed class PackageToolWindow : Package
    

    此注册工具窗口如停靠要打开的 PersistedWindowPane 到 解决方案资源管理器。 有关更多信息,请参见 如何:注册工具窗口

  3. 使用 FindToolWindow 查找工具窗口窗格或创建它,如果它已不存在。

    ' Get the (only) instance of this tool window. 
    ' The last flag is set to true so that if the tool window does not exists it will be created. 
    Dim window As ToolWindowPane = Me.FindToolWindow(GetType(MyToolWindow), 0, True)
    If (window Is Nothing) Or (window.Frame Is Nothing) Then 
        Throw New NotSupportedException(Resources.CanNotCreateWindow)
    End If
    
    // Get the (only) instance of this tool window. 
    // The last flag is set to true so that if the tool window does not exists it will be created.
    ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
    if ((window == null) || (window.Frame == null))
    {
        throw new NotSupportedException(Resources.CanNotCreateWindow);
    }
    
  4. 获取工具窗口窗格的工具窗架。

    Dim windowFrame As IVsWindowFrame = TryCast(window.Frame, IVsWindowFrame)
    
    IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
    
  5. 显示工具窗口。

    ErrorHandler.ThrowOnFailure(windowFrame.Show())
    
    ErrorHandler.ThrowOnFailure(windowFrame.Show());
    

请参见

概念

VSPackage Essentials