如何:在“工具”菜单中公开外接程序 (Visual Basic)

当使用**“外接程序向导”创建外接程序并选择将其显示为命令的选项时,默认情况下该命令位于“工具”菜单。 但是,如果创建外接程序时跳过该选项,则只需再次运行“外接程序向导”**,选中该选项,然后将现有的代码复制到新的外接程序中。

即使不可能完成,但是下面的过程会完成相同的任务。

提示

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

向现有外接程序添加菜单命令

  1. 在外接程序的 Connect 类中,添加 Implements IDTCommandTarget。

    它使您可以访问创建命令时所需的 IDTCommandTarget 命令接口。

  2. OnConnection 过程中,添加以下内容:

    Imports System
    Imports Microsoft.VisualStudio.CommandBars
    Imports Extensibility
    Imports EnvDTE
    Imports EnvDTE80
    
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    If connectMode = ext_ConnectMode.ext_cm_Startup Then
        Dim commands As Commands2 = CType(_applicationObject.Commands, _
          Commands2)
        Dim toolsMenuName As String
        Try
            Dim resourceManager As System.Resources.ResourceManager = _
            New System.Resources.ResourceManager _
             ("MyAddin1.CommandBar", System.Reflection.Assembly. _
             GetExecutingAssembly())
    
            Dim cultureInfo As System.Globalization.CultureInfo = New _
             System.Globalization.CultureInfo(_applicationObject. _
             LocaleID)
            toolsMenuName = resourceManager.GetString(String.Concat _
              (cultureInfo.TwoLetterISOLanguageName, "Tools"))
    
        Catch e As Exception
            toolsMenuName = "Tools"
        End Try
    
        Dim commandBars As CommandBars = _
          CType(_applicationObject.CommandBars, CommandBars)
        Dim menuBarCommandBar As CommandBar = _
          commandBars.Item("MenuBar")
    
        Dim toolsControl As CommandBarControl = _
          menuBarCommandBar.Controls.Item(toolsMenuName)
        Dim toolsPopup As CommandBarPopup = CType(toolsControl, _
          CommandBarPopup)
    
        Try
            Dim command As Command = _
              commands.AddNamedCommand2(_addInInstance, "MyAddin1", _
              "MyAddin1", "Executes the command for MyAddin1", True, _
              59, Nothing, CType(vsCommandStatus. _
              vsCommandStatusSupported, Integer) + _
              CType(vsCommandStatus.vsCommandStatusEnabled,  _
              Integer), vsCommandStyle.vsCommandStylePictAndText, _
              vsCommandControlType.vsCommandControlTypeButton)
    
           command.AddControl(toolsPopup.CommandBar, 1)
        Catch argumentException As System.ArgumentException
        End Try
    End If
    

    在 Visual Studio 中加载(“连接”)外接程序时,此代码将执行。 该代码确定外接程序是否使用 ext_cm_UISetup 的 ext_ConnectMode 值加载。 这意味着外接程序自安装后首次被启动。 如果为 true,则使用 AddNamedCommand 方法在**“工具”**菜单上为该外接程序创建命令。 有关更多信息,请参见 如何:添加和处理命令

  3. 将下面的两个过程添加到 Connect 类中。

    更新该命令的可用性时调用 QueryStatus 方法。 调用该命令时调用 Exec 方法。

    Public Sub QueryStatus(ByVal commandName As String,  _
      ByVal neededText As vsCommandStatusTextWanted, ByRef status As _
      vsCommandStatus, ByRef commandText As Object) Implements _
      IDTCommandTarget.QueryStatus
        If neededText = vsCommandStatusTextWanted. _
          vsCommandStatusTextWantedNone Then
            If commandName = "MyAddin1.Connect.MyAddin1" 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 = "MyAddin1.Connect.MyAddin1" Then
                handled = True
                Exit Sub
            End If
        End If
    End Sub
    

    每次实现 IDTCommandTarget 时,必须添加这两个过程。 一个执行此操作的快速方法是在编辑器左上角的**“类名”下拉框中选择 IDTCommandTarget。 在右上角的“方法名”**下拉框中依次选择各个过程。 这将用正确的参数创建可以添加代码的必需的空过程。

    用户单击菜单命令时调用 Exec 过程,因此在此处插入要在此时执行的代码。

请参见

任务

如何:在“工具”菜单上公开外接程序 (Visual C#)

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

如何:创建外接程序

概念

自动化对象模型图表