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

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

即使不可能完成,下面的过程也会产生相同的结果。

提示

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

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

  1. 将这些 using 语句添加包含 Connect 类的文件中。

    using Microsoft.VisualStudio.CommandBars;
    using System.Resources;
    using System.Reflection;
    using System.Globalization;
    using System.Windows.Forms;
    
  2. 更改 Connect 类声明以实现 IDTCommandTarget

  3. OnConnection() 过程代码替换为或更改为以下代码:

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        if(connectMode == ext_ConnectMode.ext_cm_UISetup)
        {
            object []contextGUIDS = new object[] { };
            Commands2 commands = (Commands2)_applicationObject.Commands;
            string toolsMenuName;
    
            try
            {
                ResourceManager resourceManager = new     
                  ResourceManager("MyAddin1.CommandBar",  
                  Assembly.GetExecutingAssembly());
                CultureInfo cultureInfo = new 
                  System.Globalization.CultureInfo
                  (_applicationObject.LocaleID);
                string resourceName = String.Concat(cultureInfo.
                  TwoLetterISOLanguageName, "Tools");
                toolsMenuName = resourceManager.GetString(resourceName);
            }
            catch
            {
                toolsMenuName = "Tools";
            }
    
            CommandBar menuBarCommandBar = 
              ((CommandBars)_applicationObject.CommandBars)
              ["MenuBar"];
    
              CommandBarControl toolsControl = 
                menuBarCommandBar.Controls[toolsMenuName];
              CommandBarPopup toolsPopup = 
                (CommandBarPopup)toolsControl;
    
              try
              {
                  Command command = commands.AddNamedCommand2
                    (_addInInstance, "MyAddin1", "MyAddin1", "Executes  
                    the command for MyAddin1", true, 59, ref 
                    contextGUIDS, (int)vsCommandStatus.
                    vsCommandStatusSupported+(int)vsCommandStatus.
                    vsCommandStatusEnabled, (int)vsCommandStyle.
                    vsCommandStylePictAndText, vsCommandControlType.
                    vsCommandControlTypeButton);
    
                  if((command != null) && (toolsPopup != null))
                  {
                         command.AddControl(toolsPopup.CommandBar, 1);
                  }
            }
            catch(System.ArgumentException)
            {
            }
        }
    }
    
  4. 添加下面两个所需的过程 QueryStatusExec

    public void QueryStatus(string commandName, 
      vsCommandStatusTextWanted neededText, ref vsCommandStatus status, 
      ref object commandText)
    {
        if(neededText == 
          vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
        {
            if(commandName == "MyAddin1.Connect.MyAddin1")
            {
                status = (vsCommandStatus)vsCommandStatus.
                  vsCommandStatusSupported|vsCommandStatus.
                  vsCommandStatusEnabled;
                return;
            }
        }
    }
    
    public void Exec(string commandName, vsCommandExecOption 
      executeOption, ref object varIn, ref object varOut, ref bool 
      handled)
    {
        handled = false;
        if(executeOption ==  
          vsCommandExecOption.vsCommandExecOptionDoDefault)
        {
            if(commandName == "MyAddin1.Connect.MyAddin1")
            {
                handled = true;
                    System.Windows.Forms.MessageBox.
                      Show("add-in running.");
                return;
            }
        }
    }
    

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

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

请参见

任务

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

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

如何:创建外接程序

概念

自动化对象模型图表