Compartilhar via


Como: Expor um suplemento sistema autônomo um botão na barra de ferramentas

Se você escolher a opção para criar uma interface de usuário (UI) quando o assistente Add-in para criar o suplemento, em seguida, o assistente cria um comando para o suplemento sobre o Ferramentas menu.Se você desejar exibir o suplemento em um local mais proeminente ou com com facilidade acessível — sistema autônomo sobre o principalVisual Studiobarra de ferramentas, também conhecido sistema autônomo "padrão" barra de ferramentas — e, em seguida, você pode fazer isso também.

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.

Procedimento

Para exibir um suplemento na barra de ferramentas padrão

  1. Crie ou abra um projeto suplemento.

  2. Substitua código do suplemento o código a seguir.

Exemplo

O exemplo a seguir demonstra como criar um suplemento que adiciona um botão sobre o Visual Studiobarra de ferramentas "padrão". (Esse é o nome da barra de ferramentas no Visual Studio.)

  1. Você usa o AddNamedCommand2 método para criar um comando para o suplemento.

  2. Você então obter uma referência para a barra de ferramentas padrão.

  3. Por fim, use o AddControl método para adicionar um novo botão.

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 if it does not already exist.
                    If cmd Is Nothing Then
                        cmd = _applicationObject.Commands. _
                          AddNamedCommand(_addInInstance, _
                          "ANewCommand", "ANewCommand", _
                          "A new command", True, 59, Nothing, _
                          vsCommandStatus.vsCommandStatusSupported _
                          Or vsCommandStatus.vsCommandStatusEnabled)
                    End If

                    ' 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

Consulte também

Tarefas

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

Conceitos

Exibindo suplementos em barras de ferramentas e menus

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

Referência

O Visual Studio comandos e opções

Outros recursos

Criando suplementos e assistentes