如何:在快捷菜单中公开外接程序

更新:2007 年 11 月

尽管 Visual Studio 自动化模型使得在顶级菜单上(如在“工具”菜单上)放置外接程序命令变得轻松自如,但是您也可以将命令添加到快捷菜单和子菜单上。

不过,要执行此操作,您必须使用 Microsoft Visual Studio 命令栏对象模型显式定义目标快捷菜单和子菜单。然后,您必须调用 Visual StudioAddControl 方法。

快捷菜单与 Visual Studio 中的其他菜单类似。要访问它们,请指向下拉菜单中的向下箭头,或者右击集成开发环境 (IDE) 中的某项。

若要将命令添加到快捷菜单(或任何菜单或工具栏),必须首先知道它的命令名。您可以通过在“工具”菜单上的“选项”对话框中的“键盘”节点中搜索来找到命令名。一个可用的外接程序示例搜索一个特定的菜单项,然后返回其命令名以及其他相关信息。此外接程序示例名为“Command Browse Add-in”,它位于 Visual Studio Automation Samples(Visual Studio 自动化示例)网站上。(若要访问该外接程序,请解压缩该项目,生成它,并运行随附的 AddinReg.reg 文件,然后在“工具”菜单上单击“命令浏览器”命令。)

以下过程演示了如何将外接程序命令添加到“任务列表”的快捷菜单中。

ms165628.alert_note(zh-cn,VS.90).gif说明:

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您的当前设置或版本。这些过程是使用“常规开发设置”开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

将外接程序命令添加到快捷菜单

  1. 在“文件”菜单上,指向“新建”,然后单击“项目”。

  2. 在“新建项目”对话框中,展开“其他项目类型”,单击“扩展性”,然后在“模板”窗格中单击“Visual Studio 外接程序”。

    将外接程序命名为 ContextCmd 并单击“确定”以启动“Visual Studio 外接程序向导”。

  3. 通过选中“是否为您的外接程序创建命令栏用户界面?”框,选择为外接程序创建用户界面 (UI) 的选项。

    此操作将一些 UI 代码添加到 OnConnection 方法中。它还添加了 Exec 方法(用于处理单击外接程序命令时触发的事件)和 QueryStatus 方法(用于提供有关外接程序状态的信息)。

  4. 将代码替换为下面的内容:

    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. 将单击命令时希望运行的代码添加到 Exec 过程中。

  6. 生成外接程序,然后运行它。

  7. 通过单击“视图”菜单上的“任务列表”,显示“任务列表”。

  8. 在“工具”菜单上,单击“外接程序管理器”。

  9. 通过在“外接程序管理器”中选中 ContextCmd 外接程序旁边的框,激活该外接程序。

  10. 右击“任务列表”。

    “ContextCmd”外接程序命令显示在快捷菜单上。

请参见

任务

如何:使用外接程序管理器控制外接程序

如何:创建外接程序

演练:创建向导

概念

在工具栏和菜单上显示外接程序

外接程序注册

自动化对象模型图表

参考

Visual Studio 命令和开关

其他资源

创建外接程序和向导