2010-10-21 191 views
2

是否可以在加载项中的Visual Studio中的主菜单栏中添加自定义菜单?将菜单添加到加载项中的Visual Studio菜单栏

我希望加载项创建一个公司特定的菜单,如果它尚不存在,然后将其自己的特定命令添加到该菜单。这样,如果提供了多个加载项,那么它们都可以将这些命令添加到相同的菜单中。

我发现一个msdn link为创建一个VSPackage的演练,它做到了这一点,但不是在一个加载项中,它需要它自己的特定安装/注册。

+0

菜单和子菜单以及分隔线吗? – Kiquenet 2015-02-05 08:17:59

回答

4
public static CommandBarControl GetCustomMenu(DTE2 applicationObject) 
    { 
     //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items 
     CommandBar menuBarCommandBar = ((CommandBars)applicationObject.CommandBars)["MenuBar"]; 

     //Find the Tools command bar on the MenuBar command bar as we want to add it before it 
     CommandBarControl toolsControl = menuBarCommandBar.Controls["Tools"]; 

     CommandBarControl myMenu; 

     try 
     { 
      // Get the menu bar if it already exists 
      myMenu = menuBarCommandBar.Controls["My Menu"]; 
     } 
     catch (Exception) 
     { 
      // Doesnt exist so crate a new one. 
      myMenu = menuBarCommandBar.Controls.Add(Type: MsoControlType.msoControlPopup, Id: 1234567890, Before: toolsControl.Index - 1); 
      myMenu.Caption = "My Menu"];; 
     } 
     return myMenu; 
    } 
相关问题