现在的位置: 首页 > 综合 > 正文

HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in

2013年02月07日 ⁄ 综合 ⁄ 共 23061字 ⁄ 字号 评论关闭

http://www.mztools.com/articles/2005/MZ2005003.aspx

 

public class Connect : Extensibility.IDTExtensibility2, IDTCommandTarget
{
   // Constants for command properties
   private const string MY_COMMAND_NAME = "MyCommand";
   private const string MY_COMMAND_CAPTION = "My command";
   private const string MY_COMMAND_TOOLTIP = "My command tooltip";

   // Variables for IDE and add-in instances
   private EnvDTE.DTE applicationObject;
   private EnvDTE.AddIn addInInstance;

   // Buttons that will be created on built-in commandbars of Visual Studio
   // We must keep them at class level to remove them when the add-in is unloaded
   private CommandBarButton myStandardCommandBarButton;
   private CommandBarButton myToolsCommandBarButton;
   private CommandBarButton myCodeWindowCommandBarButton;

   // CommandBars that will be created by the add-in
   // We must keep them at class level to remove them when the add-in is unloaded
   private CommandBar myTemporaryToolbar;
   private CommandBarPopup myTemporaryCommandBarPopup1;
   private CommandBarPopup myTemporaryCommandBarPopup2;

   public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode,
      object addInInst, ref System.Array custom)
   {
      try
      {
         applicationObject = (EnvDTE.DTE)application;
         addInInstance = (EnvDTE.AddIn)addInInst;

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_UISetup:

               // Do nothing for this add-in with temporary user interface
               break;

            case ext_ConnectMode.ext_cm_Startup:

               // The add-in was marked to load on startup
               // Do nothing at this point because the IDE may not be fully initialized
               // Visual Studio will call OnStartupComplete when fully initialized
               break;

            case ext_ConnectMode.ext_cm_AfterStartup:

               // The add-in was loaded by hand after startup using the Add-In Manager
               // Initialize it in the same way that when is loaded on startup
               AddTemporaryUI();
               break;
         }
      }
      catch (System.Exception e)
      {
         System.Windows.Forms.MessageBox.Show(e.ToString());
      }
   }

   public void OnStartupComplete(ref System.Array custom)
   {
      AddTemporaryUI();
   }

   public void AddTemporaryUI()
   {
      // Constants for names of built-in commandbars of Visual Studio
      const string VS_STANDARD_COMMANDBAR_NAME = "Standard";
      const string VS_MENUBAR_COMMANDBAR_NAME = "MenuBar";
      const string VS_TOOLS_COMMANDBAR_NAME = "Tools";
      const string VS_CODE_WINDOW_COMMANDBAR_NAME = "Code Window";

      // Constants for names of commandbars created by the add-in
      const string MY_TEMPORARY_COMMANDBAR_POPUP1_NAME = "MyTemporaryCommandBarPopup1";
      const string MY_TEMPORARY_COMMANDBAR_POPUP2_NAME = "MyTemporaryCommandBarPopup2";

      // Constants for captions of commandbars created by the add-in
      const string MY_TEMPORARY_COMMANDBAR_POPUP1_CAPTION = "My sub menu";
      const string MY_TEMPORARY_COMMANDBAR_POPUP2_CAPTION = "My main menu";
      const string MY_TEMPORARY_TOOLBAR_CAPTION = "My toolbar";

      // The only command that will be created. We will create several buttons from it
      Command myCommand = null;

      // Built-in commandbars of Visual Studio
      CommandBar standardCommandBar = null;
      CommandBar menuCommandBar = null;
      CommandBar toolsCommandBar = null;
      CommandBar codeCommandBar = null;

      // Buttons that will be created on a toolbars/commandbar popups created by the add-in
      // We don't need to keep them at class level to remove them when the add-in is unloaded 
      // because we will remove the whole toolbars/commandbar popups
      CommandBarButton myToolBarButton = null;
      CommandBarButton myCommandBarPopup1Button = null;
      CommandBarButton myCommandBarPopup2Button = null;

      // The collection of Visual Studio commandbars
      CommandBars commandBars = null;

      // Other variables
      CommandBarControl toolsCommandBarControl = null;
      int position = 0;
      object[] contextUIGuids = new object[] { };

      try
      {
         // ------------------------------------------------------------------------------------

         // Try to retrieve the command, just in case it was already created, ignoring the 
         // exception that would happen if the command was not created yet.
         try
         {
            myCommand = applicationObject.Commands.Item(addInInstance.ProgID + "." + MY_COMMAND_NAME, -1);
         }
         catch
         {
         }

         // Add the command if it does not exist
         if (myCommand == null)
         {
            myCommand = applicationObject.Commands.AddNamedCommand(addInInstance,
               MY_COMMAND_NAME, MY_COMMAND_CAPTION, MY_COMMAND_TOOLTIP, true, 59, ref contextUIGuids,
               (int)(vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled));
         }
         // ------------------------------------------------------------------------------------

         // Retrieve the collection of commandbars
         // Note:
         // - In VS.NET 2002/2003 (which uses the Office.dll reference) 
         //   DTE.CommandBars returns directly a CommandBars type, so a cast 
         //   to CommandBars is redundant
         // - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
         //   DTE.CommandBars returns an Object type, so we do need a cast to CommandBars
         commandBars = (CommandBars)applicationObject.CommandBars;

         // Retrieve some built-in commandbars
         standardCommandBar = commandBars[VS_STANDARD_COMMANDBAR_NAME];
         menuCommandBar = commandBars[VS_MENUBAR_COMMANDBAR_NAME];
         toolsCommandBar = GetCommandBarPopup(menuCommandBar, VS_TOOLS_COMMANDBAR_NAME);
         codeCommandBar = commandBars[VS_CODE_WINDOW_COMMANDBAR_NAME];

         // ------------------------------------------------------------------------------------

         // Create the buttons from the commands
         // Note:
         // - In VS.NET 2002/2003 (which uses the Office.dll reference) 
         //   Command.AddControl returns directly a CommandBarControl type, so a cast 
         //   to CommandBarControl is redundant
         // - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
         //   Command.AddControl returns an Object type, so we do need a cast to CommandBarControl

         // ------------------------------------------------------------------------------------
         // Button on the "Standard" toolbar
         // ------------------------------------------------------------------------------------
         myStandardCommandBarButton = (CommandBarButton)myCommand.AddControl(standardCommandBar,
            standardCommandBar.Controls.Count + 1);

         // Change some button properties
         myStandardCommandBarButton.Caption = MY_COMMAND_CAPTION;
         myStandardCommandBarButton.Style = MsoButtonStyle.msoButtonIcon; // It could be also msoButtonIconAndCaption
         myStandardCommandBarButton.BeginGroup = true; // Separator line above button

         // ------------------------------------------------------------------------------------
         // Button on the "Tools" menu
         // ------------------------------------------------------------------------------------

         // Add a button to the built-in "Tools" menu
         myToolsCommandBarButton = (CommandBarButton)myCommand.AddControl(toolsCommandBar,
            toolsCommandBar.Controls.Count + 1);

         // Change some button properties
         myToolsCommandBarButton.Caption = MY_COMMAND_CAPTION;
         myToolsCommandBarButton.BeginGroup = true; // Separator line above button

         // ------------------------------------------------------------------------------------
         // Button on the "Code Window" context menu
         // ------------------------------------------------------------------------------------

         // Add a button to the built-in "Code Window" context menu
         myCodeWindowCommandBarButton = (CommandBarButton)myCommand.AddControl(codeCommandBar,
            codeCommandBar.Controls.Count + 1);

         // Change some button properties
         myCodeWindowCommandBarButton.Caption = MY_COMMAND_CAPTION;
         myCodeWindowCommandBarButton.BeginGroup = true; // Separator line above button

         // ------------------------------------------------------------------------------------
         // New toolbar
         // ------------------------------------------------------------------------------------

         // Add a new toolbar 
         myTemporaryToolbar = commandBars.Add(MY_TEMPORARY_TOOLBAR_CAPTION, 
            MsoBarPosition.msoBarTop, System.Type.Missing, true);

         // Add a new button on that toolbar
         myToolBarButton = (CommandBarButton)myCommand.AddControl(myTemporaryToolbar,
            myTemporaryToolbar.Controls.Count + 1);

         // Change some button properties
         myToolBarButton.Caption = MY_COMMAND_CAPTION;
         myToolBarButton.Style = MsoButtonStyle.msoButtonIconAndCaption; // It could be also msoButtonIcon

         // Make visible the toolbar
         myTemporaryToolbar.Visible = true;

         // ------------------------------------------------------------------------------------
         // New submenu under the "Tools" menu
         // ------------------------------------------------------------------------------------

         // Add a new commandbar popup 
         myTemporaryCommandBarPopup1 = (CommandBarPopup)toolsCommandBar.Controls.Add(
            MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing,
            toolsCommandBar.Controls.Count + 1, true);

         // Change some commandbar popup properties
         myTemporaryCommandBarPopup1.CommandBar.Name = MY_TEMPORARY_COMMANDBAR_POPUP1_NAME;
         myTemporaryCommandBarPopup1.Caption = MY_TEMPORARY_COMMANDBAR_POPUP1_CAPTION;

         // Add a new button on that commandbar popup
         myCommandBarPopup1Button = (CommandBarButton)myCommand.AddControl(
            myTemporaryCommandBarPopup1.CommandBar, myTemporaryCommandBarPopup1.Controls.Count + 1);

         // Change some button properties
         myCommandBarPopup1Button.Caption = MY_COMMAND_CAPTION;

         // Make visible the commandbar popup
         myTemporaryCommandBarPopup1.Visible = true;

         // ------------------------------------------------------------------------------------
         // New main menu
         // ------------------------------------------------------------------------------------

         // Calculate the position of a new commandbar popup to the right of the "Tools" menu
         toolsCommandBarControl = (CommandBarControl)toolsCommandBar.Parent;
         position = toolsCommandBarControl.Index + 1;

         // Add a new commandbar popup 
         myTemporaryCommandBarPopup2 = (CommandBarPopup)menuCommandBar.Controls.Add(
            MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, position, true);

         // Change some commandbar popup properties
         myTemporaryCommandBarPopup2.CommandBar.Name = MY_TEMPORARY_COMMANDBAR_POPUP2_NAME;
         myTemporaryCommandBarPopup2.Caption = MY_TEMPORARY_COMMANDBAR_POPUP2_CAPTION;

         // Add a new button on that commandbar popup
         myCommandBarPopup2Button = (CommandBarButton)myCommand.AddControl(
            myTemporaryCommandBarPopup2.CommandBar, myTemporaryCommandBarPopup2.Controls.Count + 1);

         // Change some button properties
         myCommandBarPopup2Button.Caption = MY_COMMAND_CAPTION;

         // Make visible the commandbar popup
         myTemporaryCommandBarPopup2.Visible = true;
      }
      catch (System.Exception e)
      {
         System.Windows.Forms.MessageBox.Show(e.ToString());
      }
   }

   private CommandBar GetCommandBarPopup(CommandBar parentCommandBar, string commandBarPopupName)
   {
      CommandBar commandBar = null;
      CommandBarPopup commandBarPopup; 

      foreach (CommandBarControl commandBarControl in parentCommandBar.Controls)
      {
         if (commandBarControl.Type == MsoControlType.msoControlPopup)
         {
            commandBarPopup = (CommandBarPopup) commandBarControl;

            if (commandBarPopup.CommandBar.Name == commandBarPopupName)
            {
               commandBar = commandBarPopup.CommandBar;
               break;
            }
         }
      }
      return commandBar;
   }

   public void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref System.Array custom)
   {
      try
      {
         switch (RemoveMode)
         {
            case ext_DisconnectMode.ext_dm_HostShutdown:
            case ext_DisconnectMode.ext_dm_UserClosed:

               if ((myStandardCommandBarButton != null))
               {
                  myStandardCommandBarButton.Delete(true);
               }

               if ((myCodeWindowCommandBarButton != null))
               {
                  myCodeWindowCommandBarButton.Delete(true);
               }

               if ((myToolsCommandBarButton != null))
               {
                  myToolsCommandBarButton.Delete(true);
               }

               if ((myTemporaryToolbar != null))
               {
                  myTemporaryToolbar.Delete();
               }

               if ((myTemporaryCommandBarPopup1 != null))
               {
                  myTemporaryCommandBarPopup1.Delete(true);
               }

               if ((myTemporaryCommandBarPopup2 != null))
               {
                  myTemporaryCommandBarPopup2.Delete(true);
               }

               break;
         }
      }
      catch (System.Exception e)
      {
         System.Windows.Forms.MessageBox.Show(e.ToString());
      }
   }

   public void OnBeginShutdown(ref System.Array custom)
   {
   }

   public void OnAddInsUpdate(ref System.Array custom)
   {
   }

   public void Exec(string cmdName, vsCommandExecOption executeOption, ref object varIn,
      ref object varOut, ref bool handled)
   {

      handled = false;

      if ((executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault))
      {
         if (cmdName == addInInstance.ProgID + "." + MY_COMMAND_NAME)
         {
            handled = true;
            System.Windows.Forms.MessageBox.Show("Command executed.");
         }
      }
   }

   public void QueryStatus(string cmdName, vsCommandStatusTextWanted neededText,
      ref vsCommandStatus statusOption, ref object commandText)
   {
      if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
      {
         if (cmdName == addInInstance.ProgID + "." + MY_COMMAND_NAME)
         {
            statusOption = (vsCommandStatus) (vsCommandStatus.vsCommandStatusEnabled | 
               vsCommandStatus.vsCommandStatusSupported);
         }
         else
         {
            statusOption = vsCommandStatus.vsCommandStatusUnsupported;
         }
      }
   }
}

For permanent commandbars:

public class Connect : Extensibility.IDTExtensibility2, IDTCommandTarget
{
   // Constants for command properties
   private const string MY_COMMAND_NAME = "MyCommand";
   private const string MY_COMMAND_CAPTION = "My command";
   private const string MY_COMMAND_TOOLTIP = "My command tooltip";

   // Variables for IDE and add-in instances
   private EnvDTE.DTE applicationObject;
   private EnvDTE.AddIn addInInstance;

   public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, 
      object addInInst, ref System.Array custom)
   {
      try
      {
         applicationObject = (EnvDTE.DTE)application;
         addInInstance = (EnvDTE.AddIn)addInInst;

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_UISetup:

               // Initialize the UI of the add-in
               AddPermanentUI();
               break;

            case ext_ConnectMode.ext_cm_Startup:

               // The add-in was marked to load on startup
               // Do nothing at this point because the IDE may not be fully initialized
               // Visual Studio will call OnStartupComplete when fully initialized
               break;

            case ext_ConnectMode.ext_cm_AfterStartup:

               // The add-in was loaded by hand after startup using the Add-In Manager
               // Initialize it in the same way that when is loaded on startup
               InitializeAddIn();
               break;
         }
      }
      catch (System.Exception e)
      {
         System.Windows.Forms.MessageBox.Show(e.ToString());
      }
   }

   public void OnStartupComplete(ref System.Array custom)
   {
      InitializeAddIn();
   }

   private void InitializeAddIn()
   {
      // Initialize non-UI add-in
   }

   private void AddPermanentUI()
   {
      // Constants for names of built-in commandbars of Visual Studio
      const string VS_STANDARD_COMMANDBAR_NAME = "Standard";
      const string VS_MENUBAR_COMMANDBAR_NAME = "MenuBar";
      const string VS_TOOLS_COMMANDBAR_NAME = "Tools";
      const string VS_CODE_WINDOW_COMMANDBAR_NAME = "Code Window";

      // Constants for names of commandbars created by the add-in
      const string MY_PERMANENT_COMMANDBAR_POPUP1_NAME = "MyPermanentCommandBarPopup1";
      const string MY_PERMANENT_COMMANDBAR_POPUP2_NAME = "MyPermanentCommandBarPopup2";

      // Constants for captions of commandbars created by the add-in
      const string MY_PERMANENT_COMMANDBAR_POPUP1_CAPTION = "My sub menu";
      const string MY_PERMANENT_COMMANDBAR_POPUP2_CAPTION = "My main menu";
      const string MY_PERMANENT_TOOLBAR_CAPTION = "My toolbar";

      // CommandBars that will be created by the add-in
      CommandBar myPermanentToolbar = null;
      CommandBar myPermanentCommandBar1 = null;
      CommandBar myPermanentCommandBar2 = null;
      CommandBarPopup myPermanentCommandBarPopup1 = null;
      CommandBarPopup myPermanentCommandBarPopup2 = null;

      // Buttons that will be created on built-in commandbars of Visual Studio
      CommandBarButton myStandardCommandBarButton = null;
      CommandBarButton myToolsCommandBarButton = null;
      CommandBarButton myCodeWindowCommandBarButton = null;

      // The only command that will be created. We will create several buttons from it
      Command myCommand = null;

      // Built-in commandbars of Visual Studio
      CommandBar standardCommandBar = null;
      CommandBar menuCommandBar = null;
      CommandBar toolsCommandBar = null;
      CommandBar codeCommandBar = null;

      // Buttons that will be created on a toolbars/commandbar popups created by the add-in
      // We don't need to keep them at class level to remove them when the add-in is unloaded 
      // because we will remove the whole toolbars/commandbar popups
      CommandBarButton myToolBarButton = null;
      CommandBarButton myCommandBarPopup1Button = null;
      CommandBarButton myCommandBarPopup2Button = null;

      // The collection of Visual Studio commandbars
      CommandBars commandBars = null;

      // Other variables
      CommandBarControl toolsCommandBarControl = null;
      int position = 0;
      object[] contextUIGuids = new object[] { };

      try
      {
         // ------------------------------------------------------------------------------------

         // Try to retrieve the command, just in case it was already created, ignoring the 
         // exception that would happen if the command was not created yet.
         try
         {
            myCommand = applicationObject.Commands.Item(addInInstance.ProgID + "." + MY_COMMAND_NAME, -1);
         }
         catch
         {
         }

         // Add the command if it does not exist
         if (myCommand == null)
         {
            myCommand = applicationObject.Commands.AddNamedCommand(addInInstance, MY_COMMAND_NAME,
               MY_COMMAND_CAPTION, MY_COMMAND_TOOLTIP, true, 59, ref contextUIGuids, 
               (int) (vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled));
         }

         // ------------------------------------------------------------------------------------

         // Retrieve the collection of commandbars
         // Note:
         // - In VS.NET 2002/2003 (which uses the Office.dll reference) 
         //   DTE.CommandBars returns directly a CommandBars type, so a cast 
         //   to CommandBars is redundant
         // - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
         //   DTE.CommandBars returns an Object type, so we do need a cast to CommandBars
         commandBars = (CommandBars)applicationObject.CommandBars;

         // Retrieve some built-in commandbars
         standardCommandBar = commandBars[VS_STANDARD_COMMANDBAR_NAME];
         menuCommandBar = commandBars[VS_MENUBAR_COMMANDBAR_NAME];
         toolsCommandBar = GetCommandBarPopup(menuCommandBar, VS_TOOLS_COMMANDBAR_NAME);
         codeCommandBar = commandBars[VS_CODE_WINDOW_COMMANDBAR_NAME];

         // ------------------------------------------------------------------------------------

         // Create the buttons from the commands
         // Note:
         // - In VS.NET 2002/2003 (which uses the Office.dll reference) 
         //   Command.AddControl returns directly a CommandBarControl type, so a cast 
         //   to CommandBarControl is redundant
         // - In VS 2005 or higher (which uses the new Microsoft.VisualStudio.CommandBars.dll reference) 
         //   Command.AddControl returns an Object type, so we do need a cast to CommandBarControl

         // ------------------------------------------------------------------------------------
         // Button on the "Standard" toolbar
         // ------------------------------------------------------------------------------------

         // Add a button to the built-in "Standard" toolbar
         myStandardCommandBarButton = (CommandBarButton)myCommand.AddControl(standardCommandBar, 
            standardCommandBar.Controls.Count + 1);

         // Change some button properties
         myStandardCommandBarButton.Caption = MY_COMMAND_CAPTION;
         myStandardCommandBarButton.Style = MsoButtonStyle.msoButtonIcon; // It could be also msoButtonIconAndCaption
         myStandardCommandBarButton.BeginGroup = true; // Separator line above button

         // ------------------------------------------------------------------------------------
         // Button on the "Tools" menu
         // ------------------------------------------------------------------------------------

         // Add a button to the built-in "Tools" menu
         myToolsCommandBarButton = (CommandBarButton)myCommand.AddControl(toolsCommandBar, 
            toolsCommandBar.Controls.Count + 1);

         // Change some button properties
         myToolsCommandBarButton.Caption = MY_COMMAND_CAPTION;
         myToolsCommandBarButton.BeginGroup = true; // Separator line above button

         // ------------------------------------------------------------------------------------
         // Button on the "Code Window" context menu
         // ------------------------------------------------------------------------------------

         // Add a button to the built-in "Code Window" context menu
         myCodeWindowCommandBarButton = (CommandBarButton)myCommand.AddControl(codeCommandBar, 
            codeCommandBar.Controls.Count + 1);

         // Change some button properties
         myCodeWindowCommandBarButton.Caption = MY_COMMAND_CAPTION;
         myCodeWindowCommandBarButton.BeginGroup = true; // Separator line above button

         // ------------------------------------------------------------------------------------
         // New toolbar
         // ------------------------------------------------------------------------------------

         // Try to delete the commandbar if it exists from a previous execution, 
         // because the /resetaddin command-line switch of VS 2005 (or higher) add-in 
         // projects only resets commands and buttons, not commandbars
         try
         {
            commandBars[MY_PERMANENT_TOOLBAR_CAPTION].Delete();
         }
         catch
         {
         }

         // Add the new toolbar
         myPermanentToolbar = (CommandBar)applicationObject.Commands.AddCommandBar(MY_PERMANENT_TOOLBAR_CAPTION, 
            vsCommandBarType.vsCommandBarTypeToolbar, null, 1);
         myPermanentToolbar.Position = MsoBarPosition.msoBarTop;
         myPermanentToolbar.RowIndex = 10; // Hopefully the last one

         // Make the toolbar visible
         myPermanentToolbar.Visible = true;

         // Add a new button on that toolbar
         myToolBarButton = (CommandBarButton)myCommand.AddControl(myPermanentToolbar, 
            myPermanentToolbar.Controls.Count + 1);

         // Change some button properties
         myToolBarButton.Caption = MY_COMMAND_CAPTION;
         myToolBarButton.Style = MsoButtonStyle.msoButtonIconAndCaption; // It could be also msoButtonIcon

         // ------------------------------------------------------------------------------------
         // New submenu under the "Tools" menu
         // ------------------------------------------------------------------------------------

         // Try to delete the commandbar if it exists from a previous execution, 
         // because the /resetaddin command-line switch of VS 2005 (or higher) add-in 
         // projects only resets commands and buttons, not commandbars
         try
         {
            commandBars[MY_PERMANENT_COMMANDBAR_POPUP1_NAME].Delete();
            toolsCommandBar.Controls[MY_PERMANENT_COMMANDBAR_POPUP1_CAPTION].Delete(false);
         }
         catch
         {
         }

         // Add a new commandbar popup 
         myPermanentCommandBar1 = (CommandBar)applicationObject.Commands.AddCommandBar(
            MY_PERMANENT_COMMANDBAR_POPUP1_NAME, vsCommandBarType.vsCommandBarTypeMenu, 
            toolsCommandBar, toolsCommandBar.Controls.Count + 1);

         // Get the actual commandbar popup
         myPermanentCommandBarPopup1 = (CommandBarPopup)myPermanentCommandBar1.Parent;

         // Change some commandbar popup properties
         myPermanentCommandBarPopup1.Caption = MY_PERMANENT_COMMANDBAR_POPUP1_CAPTION;

         // Add a new button on that commandbar popup
         myCommandBarPopup1Button = (CommandBarButton)myCommand.AddControl(myPermanentCommandBar1, 
            myPermanentCommandBar1.Controls.Count + 1);

         // Change some button properties
         myCommandBarPopup1Button.Caption = MY_COMMAND_CAPTION;

         // ------------------------------------------------------------------------------------
         // New main menu
         // ------------------------------------------------------------------------------------

         // Calculate the position of a new command bar popup to the right of the "Tools" menu
         toolsCommandBarControl = (CommandBarControl)toolsCommandBar.Parent;
         position = toolsCommandBarControl.Index + 1;

         // Try to delete the commandbar if it exists from a previous execution, 
         // because the /resetaddin command-line switch of VS 2005 (or higher) add-in 
         // projects only resets commands and buttons, not commandbars
         try
         {
            commandBars[MY_PERMANENT_COMMANDBAR_POPUP2_NAME].Delete();
            menuCommandBar.Controls[MY_PERMANENT_COMMANDBAR_POPUP2_CAPTION].Delete(false);
         }
         catch
         {
         }

         // Add a new commandbar popup 
         myPermanentCommandBar2 = (CommandBar)applicationObject.Commands.AddCommandBar(
            MY_PERMANENT_COMMANDBAR_POPUP2_NAME, vsCommandBarType.vsCommandBarTypeMenu, 
            menuCommandBar, position);

         // Get the actual commandbar popup
         myPermanentCommandBarPopup2 = (CommandBarPopup)myPermanentCommandBar2.Parent;

         // Change some commandbar popup properties
         myPermanentCommandBarPopup2.Caption = MY_PERMANENT_COMMANDBAR_POPUP2_CAPTION;

         // Add a new button on that commandbar popup
         myCommandBarPopup2Button = (CommandBarButton)myCommand.AddControl(myPermanentCommandBar2, 
            myPermanentCommandBar2.Controls.Count + 1);

         // Change some button properties
         myCommandBarPopup2Button.Caption = MY_COMMAND_CAPTION;
      }

      catch (System.Exception e)
      {
         System.Windows.Forms.MessageBox.Show(e.ToString());
      }
   }
   
   private CommandBar GetCommandBarPopup(CommandBar parentCommandBar, string commandBarPopupName)
   {
      CommandBar commandBar = null;
      CommandBarPopup commandBarPopup; 

      foreach (CommandBarControl commandBarControl in parentCommandBar.Controls)
      {
         if (commandBarControl.Type == MsoControlType.msoControlPopup)
         {
            commandBarPopup = (CommandBarPopup) commandBarControl;

            if (commandBarPopup.CommandBar.Name == commandBarPopupName)
            {
               commandBar = commandBarPopup.CommandBar;
               break;
            }
         }
      }
      return commandBar;
   }

   public void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref System.Array custom)
   {
   }

   public void OnBeginShutdown(ref System.Array custom)
   {
   }

   public void OnAddInsUpdate(ref System.Array custom)
   {
   }

   public void Exec(string cmdName, vsCommandExecOption executeOption, ref object varIn,
      ref object varOut, ref bool handled)
   {
      handled = false;

      if ((executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault))
      {
         if (cmdName == addInInstance.ProgID + "." + MY_COMMAND_NAME)
         {
            handled = true;
            System.Windows.Forms.MessageBox.Show("Command executed.");
         }
      }
   }

   public void QueryStatus(string cmdName, vsCommandStatusTextWanted neededText, 
      ref vsCommandStatus statusOption, ref object commandText)
   {
      if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
      {
         if (cmdName == addInInstance.ProgID + "." + MY_COMMAND_NAME)
         {
            statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusEnabled | 
               vsCommandStatus.vsCommandStatusSupported);
         }
         else
         {
            statusOption = vsCommandStatus.vsCommandStatusUnsupported;
         }
      }
   }
}

抱歉!评论已关闭.