Porady: udostępnianie dodatku jako przycisk na pasku narzędzi

Dodatki Visual Studio zostały zaniechane w programie Visual Studio 2013.Dodatki należy uaktualniać do pakietu rozszerzenia VSPackage.Aby uzyskać więcej informacji na temat uaktualniania, zobacz Często zadawane pytania: konwertowanie dodatków na rozszerzenia pakietu VSPackage.

Jeśli wybierzesz opcję do tworzenia interfejsu użytkownika (UI) podczas korzystania z Kreatora dodatku w celu utworzenia dodatku, wówczas kreator tworzy polecenie dla dodatku w menu Narzędzia.Jeśli chcesz wyświetlić dodatek w bardziej widocznym lub łatwiej dostępnym miejscu — takim jak na głównym pasku narzędzi Visual Studio, znanym również jako „standardowy” pasek narzędzi — można to również zrobić.

[!UWAGA]

Polecenia menu i okien dialogowych mogą różnić się od tych opisanych w Pomocy, w zależności od ustawień aktywnych lub wydania.Procedury te zostały opracowane z ogólnych ustawień projektowych active.Aby zmienić swoje ustawienia, wybierz Importuj i eksportujustawienia w menu Narzędzia.Aby uzyskać więcej informacji, zobacz Dostosowywanie ustawień środowiska deweloperskiego w Visual Studio.

Procedura

Aby wyświetlić dodatek na standardowym pasku narzędzi

  1. Utwórz lub otwórz projekt dodatku.

  2. Zastąp kod dodatków poniższym kodem.

Przykład

Poniższy przykład ilustruje sposób tworzenia dodatku, który dodaje przycisk do Visual Studio paska narzędzi "standardowy". (To jest nazwa paska narzędzi w Visual Studio.)

  1. Możesz użyć AddNamedCommand2 metody tworzenia polecenia dla dodatku.

  2. Następnie uzyskaj odwołanie do standardowego paska narzędzi.

  3. Na koniec możesz użyć metody AddControl, aby dodać nowy przycisk.

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

Zobacz też

Zadania

Porady: kontrolowanie dodatków za pomocą menedżera dodatków

Koncepcje

Wyświetlanie dodatków na paskach narzędzi i menu

Wykres modelu obiektów automatyzacji

Inne zasoby

Tworzenie dodatków i kreatorów

Visual Studio — Polecenia i przełączniki