Adding a Menu to the Visual Studio Menu Bar within an Add-In

南楼画角 提交于 2019-12-02 03:51:20

问题


Is it possible to create an add a custom menu to the main menu bar in Visual Studio within an Add-In?

I want the Add-In to create a company specific menu if it does not already exist and then add its own specific command to that menu. That way if multiple Add-Ins are supplied then they can all add the commands to the same menu.

I have found an msdn link for a walkthrough in creating a VSPackage which does this but not from within an Add-In and it requires its own specific installation/registration.


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


来源:https://stackoverflow.com/questions/3987295/adding-a-menu-to-the-visual-studio-menu-bar-within-an-add-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!