Create a new top-level menu in Visual Studio
We have an add in for VS that currently is launched from the tools menu, the add-in consists of a a UI offering the user a few option buttons, which I now want to convert to a top-level menu that would offer the same functionality.
I've read this tutorial, which helped me add a new top-level menu, but couldn't really understand the logic behind all the steps. The guide doesn't really clear what each of the steps create or how can your change the output.
What the steps create is a new top-level menu with a single item underneath it. I'm trying to create some hierarchy in my menu (i.e. Top Level -> Sub category -> Commands) but got abit lost with all the groups/menus/IDs structure. Is there any clear explanation for the structure of these files? A documentation or a tutorial? If anyone had experience in the subj开发者_如何学Pythonect and could help clear things up I would much appreciate it...I haven't tried doing hierarchical menu items, but I have had similar problems with the Visual SDK .vcst file. It is a pain. A couple of things you can do.
- Install the VS Package Editor to Visual Studio Blog Entry for it: http://blogs.msdn.com/b/visualstudio/archive/2010/09/08/introducing-the-vspackage-builder.aspx
- Download source code (open source so you can see how they do it) for an add-in that does similar things. Example is AnkhSVN which is a Subversion Repository Add-in to Visual Studio. Here is the source code: http://ankhsvn.open.collab.net/source/browse/ankhsvn/
I assume that nowadays by "add-in" you mean an extension that is a VS package (using the VS SDK) because an "add-in" was an older form of extension for VS 2013 and lower. (If you really mean an "add-in" then see my sample HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in)
Packages use .vsct files. To answer your question, see the .vsct files of my samples here:
- CommandTopMenu
- CommandSubMenu
(and to learn see also the other ones for context menus, toolbars, etc.). In the .vcst file they use "CommandPlacements" to separate the definition of an item from its "placement", and comments to explain the relationship between the 3 kinds of items:
- Menus (Main-menu/Top-menus/Sub-menus/Context menus) and Toolbars.
- Groups: a group is a container for other groups and also for commands and sub-menus.
- Commands
Remember the rules:
- The parent of a top-menu is always the Main menu of VS, never a group
- The parent of a sub-menu is always a group, never directly a toolbar or any kind of menu.
- The parent of a command is always a group, never directly a toolbar or any kind of menu (same rule that for sub-menus)
- The parent of a group can be a menu, a toolbar, a context menu, etc. and it can be also another group.
- A menu (any kind) or toolbar can be either created by your extension (except the main menu of VS) or an existing one of VS, identified by prefix "IDM_". See GUIDs and IDs of Visual Studio menus and GUIDs and IDs of Visual Studio toolbars.
- A group can be either new (created by your extension) or an existing group of Visual Studio, identified by prefix "IDG_". You have some built-in Visual Studio groups in the links above, but for a more exhaustive list install the ExtensionTools extension (Mads Kristensen) that provides intellisense in the .vsct file or check the source code of its VsctBuiltInCache.cs file.
Code example
<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="...">
<!-- Extern section unchanged -->
<Commands package="guidHowToPackagePkg">
<Menus>
<!-- New menu added -->
<Menu guid="guidBasicVSCTSampleCmdSet" id="SubMenu" priority="0x200"
type="Menu">
<Parent guid="guidBasicVSCTSampleCmdSet" id="TopLevelMenuGroup" />
<Strings>
<ButtonText>Other Commands</ButtonText>
<CommandName>Other Commands</CommandName>
</Strings>
</Menu>
</Menus>
<Groups>
<!-- Group changed to SubMenuGroup and attached to SubMenu -->
<Group guid="guidBasicVSCTSampleCmdSet" id="SubMenuGroup"
priority="0x0600">
<Parent guid="guidBasicVSCTSampleCmdSet" id="SubMenu"/>
</Group>
</Groups>
<Buttons>
<!-- We attached these two buttons to SubMenuGroup -->
<Button guid="guidBasicVSCTSampleCmdSet" id="ThirdCommand" priority="0x0100"
type="Button">
<Parent guid="guidBasicVSCTSampleCmdSet" id="SubMenuGroup" />
<Icon guid="guidImages" id="bmpPicX" />
<Strings>
<CommandName>ThirdCommand</CommandName>
<ButtonText>Third Command</ButtonText>
</Strings>
</Button>
<Button guid="guidBasicVSCTSampleCmdSet" id="FourthCommand"
priority="0x0101" type="Button">
<Parent guid="guidBasicVSCTSampleCmdSet" id="SubMenuGroup" />
<Icon guid="guidImages" id="bmpPicArrows" />
<Strings>
<CommandName>FourthCommand</CommandName>
<ButtonText>Fourth Command</ButtonText>
</Strings>
</Button>
</Buttons>
</Commands>
<Symbols>
<!-- We add a SubMenu and changed SubMenuGroup -->
<GuidSymbol name="guidBasicVSCTSampleCmdSet" value="...">
<IDSymbol name="SubMenu" value="0x0101" />
<IDSymbol name="SubMenuGroup" value="0x0201" />
</GuidSymbol>
</Symbols>
</CommandTable>
This provides you with the following top-level menu:
Here's a full chapter on the topic. This pretty much explains everything there is to know on (hierarchical) menu's.
http://dotneteers.net/blogs/divedeeper/archive/2010/05/23/vs-2010-package-development-chapter-2-commands-menus-and-toolbars.aspx
精彩评论