【问题标题】:Adding a Menu to the Visual Studio Menu Bar within an Add-In将菜单添加到加载项中的 Visual Studio 菜单栏
【发布时间】:2010-10-21 12:15:55
【问题描述】:
是否可以在加载项中向 Visual Studio 的主菜单栏添加自定义菜单?
我希望加载项创建一个公司特定的菜单(如果它尚不存在),然后将它自己的特定命令添加到该菜单。这样,如果提供了多个加载项,那么它们都可以将命令添加到同一个菜单中。
我找到了一个msdn link 来了解创建 VSPackage 的演练,该 VSPackage 执行此操作但不是从加载项内部进行,它需要自己的特定安装/注册。
【问题讨论】:
标签:
c#
.net
visual-studio-2010
add-in
menubar
【解决方案1】:
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;
}