Compartilhar via


How to: Create Reusable Groups of Buttons

A command group is a collection of commands that always appear together on a menu or toolbar. Any command group can be re-used by assigning it to different parent menus in the CommandPlacements section of the .vsct file.

Command groups typically contain buttons, but they can also contain other menus or combo boxes.

To create a reusable group of buttons

  1. Create a VSPackage by running the Visual Studio Package Template, and select Menu Command on the Select VSPackage Options page. As an alternative, you can open an existing project that contains a .vsct file. For more information, see Walkthrough: Creating a Menu Command By Using the Visual Studio Package Template.

  2. Open the .vsct file in the project.

  3. In the Symbols section, find the GuidSymbol element that contains groups and commands for the project.

    By default, it is named guid<ProjectName>CmdSet.

  4. Add an IDSymbol element for your group and one for each button that you will add to the group, as shown in the following example.

    <GuidSymbol name="guidButtonGroupCmdSet" value="{f69209e9-975a-4543-821d-1f4a2c52d737}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="cmdidMyCommand" value="0x0100" />
    </GuidSymbol>
    

    By default, the package template creates a group named MyGroup and a button that has the name that you provided, together with an IDSymbol entry for each.

  5. In the Groups section, create a new Group element that has the same guid and id attributes as those given in the Symbols section. You can also use an existing group, or use the entry that is provided by the package template, as shown in the following example.

    <Group guid="guidButtonGroupCmdSet" id="MyMenuGroup" priority="0x0600">
      <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
    </Group>
    

    The default group, which is shown in the example, appears on the Visual Studio Tools menu.

To populate a group of buttons for reuse

  1. You can put a command or menu in a group either by using the group as a parent in the definition of the command or menu, or by putting the command or menu in the group by using the CommandPlacements section.

    A button is defined as a Button element inside the Buttons section. Define a button that has your group as its parent, or use the button that is provided by the package template, as shown in the following example.

    <Button guid="guidButtonGroupCmdSet" id="cmdidMyCommand" priority="0x0100" type="Button">
      <Parent guid="guidButtonGroupCmdSet" id="MyMenuGroup" />
      <Icon guid="guidImages" id="bmpPic1" />
      <Strings>
        <CommandName>cmdidMyCommand</CommandName>
        <ButtonText>My Command name</ButtonText>
      </Strings>
    </Button>
    
  2. If a button must appear in more than one group, create an entry for it in the CommandPlacements section. Set the guid and id attributes of the CommandPlacement element to match those of the button you want to position, and then set the guid and id of its Parent element to those of the target group, as shown in the following example.

    <CommandPlacements>
      <CommandPlacement guid="guidButtonGroupCmdSet" id="cmdidMyCommand" priority="0x105">
        <Parent guid="guidButtonGroupCmdSet" id="MyMenuGroup" />
      </CommandPlacement>
    </CommandPlacements>
    

    Note

    The value of the Priority field determines the position of the command in the new command group. Priorities set in the CommandPlacement element override those set in the item definition. Commands that have lower priority values are displayed before commands that have higher priority values. Duplicate priority values are permitted, but the relative position of commands that have the same priority value cannot be guaranteed because the order in which the devenv setup command creates the final interface from the registry may not be consistent.

To put a reusable group of buttons on a menu

  • Create an entry in the CommandPlacements section. Set the GUID and ID of the CommandPlacement element to those of your group, and set the parent GUID and ID to those of the target location. Notice that a command group can be included on more than one menu. The parent menu can be one that you created, one that is supplied by Visual Studio (as described in ShellCmdDef.vsct or SharedCmdDef.vsct), or one that is defined in another VSPackage. The number of parenting layers is unlimited as long as the parent menu is eventually connected to Visual Studio or to a shortcut menu that is displayed by a VSPackage.

    The following example puts the group on the Solution Explorer toolbar, to the right of the other buttons.

    <CommandPlacements>
      <CommandPlacement guid="guidButtonGroupCmdSet" id="MyMenuGroup" 
          priority="0x605">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
      </CommandPlacement>
    </CommandPlacements>
    

See Also

Tasks

How to: Create and Handle Commands in VSPackages (C#)

Other Resources

Common Tasks with Commands, Menus, and Toolbars