Freigeben über


Gewusst wie: Verfügbarmachen eines Add-Ins im Menü "Extras" (Visual C#)

Aktualisiert: November 2007

When you create an add-in by using the Add-In Wizard and select the option to display it as a command, the command is on the Tools menu by default. If you skip this option when you create the add-in, however, you can simply run the Add-In Wizard again, check that option, and then copy your existing code to the new add-in.

If doing this is not possible, though, the following procedure produces the same result.

Hinweis:

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 Visual Studio-Einstellungen.

To add a menu command to an existing add-in

  1. Replace or change the add-in's Connect class and OnConnection() procedure code to the following:

    using Microsoft.VisualStudio.CommandBars;
    using System.Resources;
    using System.Reflection;
    using System.Globalization;
    
    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)
            {
            }
        }
    }
    
  2. Add the following two required procedures, QueryStatus and Exec:

    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;
            }
        }
    }
    

    Each time you implement IDTCommandTarget, you must add these two procedures. A quick way to do it is to select IDTCommandTarget in the Class Name drop-down box at the top-left corner of the editor. Select each procedure in turn from the Method Name drop-down box in the top-right corner. This creates the necessary empty procedures with the correct parameters to which you can then add code.

    The Exec procedure is called when a user clicks your menu command, so insert code there that you want to execute at that time.

Siehe auch

Aufgaben

Gewusst wie: Verfügbarmachen eines Add-Ins im Menü "Extras" (Visual Basic)

Gewusst wie: Steuern von Add-Ins mit dem Add-In-Manager

Gewusst wie: Erstellen von Add-Ins

Konzepte

Diagramm "Automationsobjektmodell"