Akos Nagy
Akos Nagy

Reputation: 4350

Visual Studio Extension with custom command and subcommands

I'm trying to create a simple Visual Studio Extension using Visual Studio 2017 that adds commands to the project context menu in the Solution Explorer. I'd like to add my commands grouped together in a main option and then from that main option I would like my commands to be accessible, just like how the Add->New item, Add->Existing item, Add->Class etc. works for in the context menu of the project.

After going through the documentation here's what I have come up with so far for my vsct file:

<Commands package="guidMainItemPackage">      
  <Groups>
    <Group guid="guidMainItemPackageCmdSet" id="MyMenuGroup" priority="0x0600" />              
    <Group guid="guidMainItemPackageCmdSet" id="SubMenuGroup" priority="0x0601" />              
  </Groups>

  <Buttons>
    <Button guid="guidMainItemPackageCmdSet" id="MainItemId" priority="0x0100" type="Button">
      <Parent guid="guidMainItemPackageCmdSet" id="MyMenuGroup" />
      <Icon guid="guidImages" id="bmpPic1" />
      <Strings>
        <ButtonText>Invoke MainItem</ButtonText>
      </Strings>
    </Button>

    <Button guid="guidMainItemPackageCmdSet" id="cmdidSubItem" priority="0x0101" type="Button">
      <Parent guid="guidMainItemPackageCmdSet" id="SubMenuGroup" />
      <Icon guid="guidImages1" id="bmpPic1" />
      <Strings>
        <ButtonText>Invoke SubItem</ButtonText>
      </Strings>
    </Button>
  </Buttons>

  <Bitmaps>     
    <Bitmap guid="guidImages" href="Resources\MainItem.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough" />
    <Bitmap guid="guidImages1" href="Resources\SubItem.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough" />
  </Bitmaps>
</Commands>

<CommandPlacements>
  <CommandPlacement guid="guidMainItemPackageCmdSet" id="MyMenuGroup" priority="0xFFFF">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_PROJNODE" />
  </CommandPlacement>    
  <CommandPlacement guid="guidMainItemPackageCmdSet" id="SubMenuGroup" priority="0xFFFE">
    <Parent guid="guidMainItemPackageCmdSet" id="MyMenuGroup" />
  </CommandPlacement>
</CommandPlacements>

<Symbols>
  <GuidSymbol name="guidMainItemPackage" value="{30f66380-eeeb-48bf-8554-0d63d87194af}" />  
  <GuidSymbol name="guidMainItemPackageCmdSet" value="{98ee7e8f-c421-4f0e-a9e9-86eab5141563}">
    <IDSymbol name="MyMenuGroup" value="0x1020" />
    <IDSymbol name="SubMenuGroup" value="0x1028" />
    <IDSymbol name="MainItemId" value="0x0100" />
    <IDSymbol value="4129" name="cmdidSubItem" />
  </GuidSymbol>
  <GuidSymbol name="guidImages" value="{b3c2dfad-df35-4e6b-880e-8018ce6c3d0f}">
    <IDSymbol name="bmpPic1" value="1" />
    <IDSymbol name="bmpPic2" value="2" />
    <IDSymbol name="bmpPicSearch" value="3" />
    <IDSymbol name="bmpPicX" value="4" />
    <IDSymbol name="bmpPicArrows" value="5" />
    <IDSymbol name="bmpPicStrikethrough" value="6" />
  </GuidSymbol> 
  <GuidSymbol value="{93d3ad22-fe33-40da-b85f-a926952e3914}" name="guidImages1">
    <IDSymbol name="bmpPic1" value="1" />
    <IDSymbol name="bmpPic2" value="2" />
    <IDSymbol name="bmpPicSearch" value="3" />
    <IDSymbol name="bmpPicX" value="4" />
    <IDSymbol name="bmpPicArrows" value="5" />
    <IDSymbol name="bmpPicStrikethrough" value="6" />
  </GuidSymbol>
</Symbols>

So basically I have created added two Custom commands to my project, a button for each of the commands, a group for each of the buttons and a commandplacement for each of the groups (everything else is just the default generated vsct template).

My problem is that using the code above, on the first command is displayed and it has no subitems. What would be the correct vsct content to achieve the desired layout?

Upvotes: 1

Views: 525

Answers (1)

Akos Nagy
Akos Nagy

Reputation: 4350

After some more trial and error (mostly error), I decided to go through the command definitions of Visual Studio itself to find a similar group of commands, and I've finally came up with the answer. Here's the steps you need to take to realize this structure:

  1. Create a group and set the parent of this group to the Visual Studio projects context menu.
  2. Create a menu, whose parent is the group created in step 1.
  3. Create a second group, whose parent is the menu created in step 2.
  4. Create buttons for only the subitems.
  5. Create commandplacements for the subitems created in step 4 where you place each button in the group created in step 3.

Upvotes: 1

Related Questions