Compartir a través de


How to: Expose an Add-In on a Shortcut Menu

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

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.

To do this, though, you must use the Microsoft Visual Studio Command Bar object model to explicitly define the target shortcut menu and submenu. You must then call the Visual Studio AddControl method.

Shortcut menus are similar to other menus in Visual Studio.To access them, you point to a down arrow in a dropdown menu, or you right-click an item in the integrated development environment (IDE).

To add a command to a shortcut menu (or any menu or toolbar), you must first know its command name. You could find it by searching through the Keyboard node in the Options dialog box on the Tools menu.

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

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.

To add an add-in command to a shortcut menu

  1. On the File menu, point to New, and then click 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.

    This adds some UI code to the OnConnection method. It 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. Replace the code with the following:

    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. Add the code that you want to run when the command is clicked in the Exec procedure.

  6. Build the add-in and then run it.

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

  8. On the Tools menu, click Add-In Manager.

  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.

See Also

Tasks

How to: Control Add-Ins By Using the Add-In Manager

How to: Create an Add-In

Walkthrough: Creating a Wizard

Reference

PAVE Visual Studio Commands and Switches

Concepts

Displaying Add-Ins on Toolbars and Menus

Add-In Registration

Automation Object Model Chart

Other Resources

Creating Add-ins and Wizards