Compartir a través de


Cómo: Exponer un complemento como un botón en la barra de herramientas

Los complementos de Visual Studio están desusados en Visual Studio 2013. Debe actualizar los complementos a las extensiones de VSPackage. Para obtener más información sobre la actualización, vea Preguntas más frecuentes: Convertir complementos en extensiones de VSPackage.

Si elige la opción de crear una interfaz de usuario cuando utilice el Asistente para complementos para crear un complemento, el asistente creará un comando para el complemento en el menú Herramientas. Si desea mostrar el complemento en una ubicación más visible o de más fácil acceso, tal como la barra de herramientas principal de Visual Studio, también conocida como barra de herramientas "estándar", puede hacerlo también.

Nota

Los cuadros de diálogo y comandos de menú que se ven pueden diferir de los descritos en la Ayuda, en función de los valores de configuración o de edición activos.Estos procedimientos se han desarrollado con la Configuración de desarrollo general activa.Para cambiar la configuración, elija la opción Importar y Exportarconfiguraciones en el menú Herramientas.Para obtener más información, vea Personalizar la configuración de desarrollo en Visual Studio.

Procedimiento

Para mostrar un complemento en la barra de herramientas estándar

  1. Cree o abra un proyecto de complemento.

  2. Reemplace el código del complemento por el código siguiente.

Ejemplo

En el ejemplo siguiente se muestra cómo crear un complemento que agrega un botón en la barra de herramientas "estándar" de Visual Studio. (Es el nombre de la barra de herramientas en Visual Studio).

  1. Utilice el método AddNamedCommand2 para crear un comando para el complemento.

  2. A continuación, obtenga una referencia a la barra de herramientas estándar.

  3. Finalmente, utilice el método AddControl para agregar un nuevo botón.

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 stdCmdBarCtl 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 stdCmdBar As CommandBar
        Dim cmdBarBtn As CommandBarButton

        Try
            _applicationObject = CType(application, DTE2)
            _addInInstance = CType(addInInst, AddIn)

            Select Case connectMode
                Case ext_ConnectMode.ext_cm_AfterStartup, _
                  ext_ConnectMode.ext_cm_Startup
                    ' Add the command
                    cmd = _applicationObject.Commands. _
                      AddNamedCommand(_addInInstance, _
                      "ANewCommand", "ANewCommand", _
                      "A new command", True, 59, Nothing, _
                      vsCommandStatus.vsCommandStatusSupported _
                      Or vsCommandStatus.vsCommandStatusEnabled)

                    ' Reference the Visual Studio standard toolbar.
                    stdCmdBar =
                      CType(_applicationObject.CommandBars.Item _
                      ("Standard"),  _
                      Microsoft.VisualStudio.CommandBars.CommandBar)

                    ' Add a button to the standard toolbar.
                    stdCmdBarCtl = CType(cmd.AddControl(stdCmdBar, _
                       stdCmdBar.Controls.Count + 1),  _
                       Microsoft.VisualStudio.CommandBars. _
                       CommandBarControl)

                    ' Set a caption for the toolbar button.
                    stdCmdBarCtl.Caption = "A new command bar"

                    ' Set the toolbar's button style to an icon button.
                    cmdBarBtn = CType(stdCmdBarCtl, CommandBarButton)
                    cmdBarBtn.Style = MsoButtonStyle.msoButtonIcon
            End Select

        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
            ' When the add-in closes, get rid of the toolbar button.
            If Not (stdCmdBarCtl Is Nothing) Then
                stdCmdBarCtl.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 neededText = EnvDTE.vsCommandStatusTextWanted. _
          vsCommandStatusTextWantedNone Then

            If commandName = "cmdBar2.Connect.ANewCommand" Then
                status = CType(vsCommandStatus.vsCommandStatusEnabled _
                  + vsCommandStatus.vsCommandStatusSupported,  _
                  vsCommandStatus)
            Else
                status = vsCommandStatus.vsCommandStatusUnsupported
            End If
        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 = "cmdBar2.Connect.ANewCommand" Then
                handled = True
                System.Windows.Forms.MessageBox.Show("Add-in running")
                Exit Sub
            End If
        End If
    End Sub
End Class

Vea también

Tareas

Cómo: Controlar complementos con el Administrador de complementos

Conceptos

Mostrar complementos en barras de herramientas y menús

Gráfico del modelo de objetos de automatización

Otros recursos

Crear complementos y asistentes

Modificadores y comandos de Visual Studio