Condividi tramite


Procedura: Aprire una finestra degli strumenti a livello di codice

Le finestre degli strumenti in genere vengono aperte scegliendo un comando a un menu, oppure premere un tasto di scelta rapida equivalente. Tuttavia, potrebbe essere necessario aprire una finestra degli strumenti a livello di codice, ad esempio il gestore comando concessa.

Per aprire una finestra degli strumenti in un VSPackage gestito che la fornisce, utilizzare FindToolWindow. Per aprire una finestra degli strumenti in un altro package VS, FindToolWindowdi utilizzo. In entrambi i casi, la finestra degli strumenti viene creata come richiesto.

Il codice seguente viene ricavato dall'esempio c# Reference.ToolWindow.

Per aprire una finestra degli strumenti a livello di codice

  1. Creare il riquadro della finestra degli strumenti, il frame e il package VS che li implementa. Per ulteriori informazioni, vedere Procedura: Per creare una finestra degli strumenti.

  2. Registrare la finestra degli strumenti con Visual Studio aggiungendo ProvideToolWindowAttribute al package VS che fornisce.

    <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
    

    In questo modo viene registrato la finestra degli strumenti PersistedWindowPane da aprire come ancorato a Esplora soluzioni. Per ulteriori informazioni, vedere Procedura: Registrare una finestra degli strumenti.

  3. Utilizzo FindToolWindow trovare il riquadro della finestra degli strumenti o crearlo se non esiste già.

    ' 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. Ottenere il frame di finestra degli strumenti dal riquadro della finestra degli strumenti.

    Dim windowFrame As IVsWindowFrame = TryCast(window.Frame, IVsWindowFrame)
    
    IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
    
  5. Per visualizzare la finestra degli strumenti.

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

Vedere anche

Concetti

Concetti di base di un VSPackage