Udostępnij za pośrednictwem


Jak: narazić dodatek programu jako przycisk na pasku narzędzi

Jeśli zostanie wybrana opcja, aby utworzyć interfejs użytkownika (UI) podczas korzystania z kreatora Add-In do tworzenia dodatek, a następnie kreator tworzy polecenie Dodaj w sprawie Narzędzia menu.Jeśli chcesz wyświetlić dodatek w bardziej widocznym i łatwo dostępnym miejscu — takie jak na głównym Visual Studio pasek narzędzi, znany również jako "norma" pasek narzędzi — a następnie to także zrobić.

[!UWAGA]

Okien dialogowych i poleceń menu, którą widzisz mogą różnić się od tych opisanych w pomocy, w zależności od tego, aktywne ustawienia lub edition.Procedury te zostały opracowane z ogólnych ustawień rozwoju aktywnych.Aby zmienić ustawienia, wybierz polecenie przywóz i wywózUstawienia na Narzędzia menu.Aby uzyskać więcej informacji, zobacz Visual Studio, ustawienia.

Procedura

Aby wyświetlić w Dodaj w na pasku narzędzi Standardowy

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

  2. Zastąp kodu Dodaj poniższy kod.

Przykład

Poniższy przykład ilustruje sposób tworzenia dodatku, który dodaje przycisk na Visual Studio "standardowego" pasek narzędzi.(Jest to nazwa paska narzędzi w Visual Studio.)

  1. Za pomocą AddNamedCommand2 metodę tworzenia polecenia dla dodatku.

  2. Następnie uzyskać odniesienie do paska narzędzi Standardowy.

  3. Wreszcie, użyj AddControl metodę, 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

Jak: dodatki formantu przy użyciu dodać Menedżera

Informacje

Visual Studio poleceń i przełączników

Koncepcje

Wyświetlanie dodatki na paski narzędzi i menu

Wykres modelu obiektu automatyzacji

Inne zasoby

Tworzenie dodatków i kreatorów