Export (0) Print
Expand All

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

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. An available add-in sample searches for a particular menu item, and it then returns its command name and other related information. This add-in sample is named "Command Browse Add-in" and it is located on the Visual Studio Automation Samples site. (To access the add-in, unpack the project, build it, run the accompanying AddinReg.reg file, and then click the Command Browser command on the Tools menu.)

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

NoteNote:

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 Export Settings on the Tools menu. For more information, see Visual Studio Settings.

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.

Community Additions

ADD
Show:
© 2014 Microsoft