Partilhar via


Como: Expor um suplemento em um menu de atalho

While the Visual Studio automation model makes it easy to place add-in commands on top-level menus, such as on the Tools menu, you can also add commands to shortcut menus and submenus.

Equivalente de modelo de projetoYou must then call the Visual Studio AddControl method.

CharacterSetVisual Studio

CommandArgumentsYou could find it by searching through the Keyboard node in the Options dialog box on the Tools menu.DebuggerTypeThis add-in sample is named "Command Browse Add-in" and it is located on the Visual Studio Automation Samples site.(To access the add-in, unpack the project, build it, run the accompanying AddinReg.reg file, and then click the Command Browser command on the Tools menu.)

The following procedure demonstrates how to add an add-in command to the Task List's shortcut menu.

Observação:

As caixas de diálogo e comandos de menu demonstradas podem ser diferentes daqueles descritas na Ajuda, dependendo das configurações ativas ou configurações de edição.Esses procedimentos foram desenvolvidos com o Geral Development Settings ativo.Para alterar as configurações, escolher Import and ExportSettings on the Tools menu.Para obter mais informações, consulte Configurações do Visual Studio.

IntermediateDirectory

  1. No menu File, aponte para New, e em seguida, clique em Project.

  2. In the New Project dialog box, expand Other Project Types, click Extensibility, and then click Visual Studio Add-in in the Templates pane.

    Name the add-in ContextCmd and click OK to start the Visual Studio Add-in Wizard.

  3. Select the option to create a user interface (UI) for the add-in by checking the Would you like to create a command bar UI for your add-in? box.

    RemotoOnConnectionIt also adds the Exec method, which handles the event when someone clicks the add-in command, and the QueryStatus method, which provides information on the status of the add-in.

  4. Substitua o código com o seguinte:

    Imports System
    Imports Microsoft.VisualStudio.CommandBars
    Imports Extensibility
    Imports EnvDTE
    Imports EnvDTE80
    
    Public Class Connect
    
        Implements IDTExtensibility2
        Implements IDTCommandTarget
    
        Dim _applicationObject As DTE2
        Dim _addInInstance As AddIn
    
        Dim cmdBarCtl As CommandBarControl
    
        Public Sub New()
        End Sub
    
        Public Sub OnConnection(ByVal application As Object, ByVal _
          connectMode As ext_ConnectMode, ByVal addInInst As Object, _
          ByRef custom As Array) Implements _
          IDTExtensibility2.OnConnection
            Dim cmd As Command
            Dim cmdBar As CommandBar
    
            _applicationObject = CType(application, DTE2)
            _addInInstance = CType(addInInst, AddIn)
    
            Try
                If CType(ext_ConnectMode.ext_cm_AfterStartup Or _
                  ext_ConnectMode.ext_cm_Startup, Boolean) Then
                    ' If the command does not exist, add it.
                    If cmd Is Nothing Then
                        cmd = _applicationObject.Commands. _
                          AddNamedCommand(_addInInstance, _
                          "newCmd", "newCmd", "Runs the add-in.", _
                          True, 59, Nothing, _
                          vsCommandStatus.vsCommandStatusSupported _
                          Or vsCommandStatus.vsCommandStatusEnabled)
                    End If
    
                    ' Reference the Task List shortcut menu.
                    cmdBar = CType(_applicationObject. _
                      CommandBars.Item("Task List"), _
                      Microsoft.VisualStudio.CommandBars.CommandBar)
    
                    ' Add a command to the Task List's shortcut menu.
                    cmdBarCtl = CType(cmd.AddControl(cmdBar, _
                      cmdBar.Controls.Count + 1), _
                      Microsoft.VisualStudio.CommandBars. _
                      CommandBarControl)
                    cmdBarCtl.Caption = "A New Command"
                End If
            Catch e As System.Exception
                System.Windows.Forms.MessageBox.Show(e.ToString)
            End Try
        End Sub
    
        Public Sub OnDisconnection(ByVal disconnectMode As _
          ext_DisconnectMode, ByRef custom As Array) Implements _
          IDTExtensibility2.OnDisconnection
            Try
                ' Delete the command bar control from the 
                   ' shortcut menu.
                If Not (cmdBarCtl Is Nothing) Then
                    cmdBarCtl.Delete()
                End If
            Catch e As System.Exception
                System.Windows.Forms.MessageBox.Show(e.ToString)
            End Try
        End Sub
    
        Public Sub OnAddInsUpdate(ByRef custom As Array) Implements _
          IDTExtensibility2.OnAddInsUpdate
        End Sub
    
        Public Sub OnStartupComplete(ByRef custom As Array) Implements _
          IDTExtensibility2.OnStartupComplete
        End Sub
    
        Public Sub OnBeginShutdown(ByRef custom As Array) Implements _
          IDTExtensibility2.OnBeginShutdown
        End Sub
    
        Public Sub QueryStatus(ByVal commandName As String, ByVal _
          neededText As vsCommandStatusTextWanted, ByRef status As _
          vsCommandStatus, ByRef commandText As Object) Implements _
          IDTCommandTarget.QueryStatus
            If commandName = "ContextCmd.Connect.newCmd" Then
                status = CType(vsCommandStatus.vsCommandStatusEnabled _
                  + vsCommandStatus.vsCommandStatusSupported, _
                  vsCommandStatus)
            Else
                status = vsCommandStatus.vsCommandStatusUnsupported
            End If
        End Sub
    
        Public Sub Exec(ByVal commandName As String, ByVal _
          executeOption As vsCommandExecOption, ByRef varIn As _
          Object, ByRef varOut As Object, ByRef handled As Boolean) _
          Implements IDTCommandTarget.Exec
            handled = False
            If executeOption = vsCommandExecOption. _
              vsCommandExecOptionDoDefault Then
                If commandName = "ContextCmd.Connect.newCmd" Then
                    handled = True
                    System.Windows.Forms.MessageBox.Show("Add-in _
                      running...")
                End If
            End If
        End Sub
    End Class
    
  5. Computador_remotoExec

  6. SQLDebugging

  7. Display the Task List by clicking Task List on the View menu.

  8. Sobre o Ferramentas menu, clicar Gerenciador de suplemento.

  9. Activate the ContextCmd add-in by checking the box next to it in the Add-In Manager.

  10. Right-click the Task List.

    The ContextCmd add-in command appears on the shortcut menu.

Consulte também

Tarefas

Como: Add-ins de controle com o Gerenciador de suplemento

Como: Criar um suplemento

Demonstra Passo a passo: Criando um assistente

Conceitos

Exibindo suplementos em barras de ferramentas e menus

inscrição do suplemento

Gráfico do modelo de objetos de automação

Referência

O Visual Studio comandos e opções

Outros recursos

Criando suplementos e assistentes