2010-10-21 198 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; 
    } 
相關問題