问题
I am building my first VS extension, so my current skills in this area amount to following tutorials and asking questions. The extension is for encrypting/decryption a section of the web.config
file of a web app project. I have 2 commands, and currently the buttons are set up in the .vsct
file as follows:
<Buttons>
<Button guid="guidEncryptConfigCommandPackageCmdSet" id="EncryptConfigCommandId" priority="0x0100" type="Button">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="MyMenuGroup" />
<Strings>
<ButtonText>Encrypt Mail Settings</ButtonText>
</Strings>
</Button>
<Button guid="guidEncryptConfigCommandPackageCmdSet" id="cmdidDecryptConfigCommand" priority="0x0100" type="Button">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="MyMenuGroup" />
<Strings>
<ButtonText>Decrypt Mail Settings</ButtonText>
</Strings>
</Button>
</Buttons>
This gives me 2 buttons in the Tools
menu, as follows:
Encrypt Mail Settings
Decrypt Mail Settings
I would like to have just one top level button in the Tools
menu, with 2 nested buttons, one for each operation, e.g:
Config Encryptor
...Encrypt Mail Settings
...Decrypt Mail Settings
How do I achieve the outcome I am looking for?
回答1:
Related documents about this issue:
Add submenu to menu, Add menu to menu bar, GUIDs and IDs for the VS Menus.
What we want:
Click the Tools
menu in VS=>display Config Encryptor
submenu, Click the Config Encryptor
menu will display Encrypt Mail Settings
and Decrypt Mail Settings
commands.
The structure in my xx.vsct:
Tools menu in IDE
--SubMenuGroup
--SubMenu1
--ButtonsGroup
--EncryptConfigCommandId(Encrypt Mail Settings)
--DecryptConfigCommandId(Decrypt Mail Settings)
The real content in Commands
section:
<Commands package="guidEncryptConfigCommandPackage">
<Menus>
<Menu guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenu1" priority="0x0100" type="Menu">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenuGroup"/>
<Strings>
<ButtonText>Config Encryptor</ButtonText>
<CommandName>Config Encryptor</CommandName>
</Strings>
</Menu>
</Menus>
<Groups>
<Group guid="guidEncryptConfigCommandPackageCmdSet" id="ButtonsGroup" priority="0x0600">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenu1"/>
</Group>
<Group guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenuGroup" priority="0x0600">
<Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
</Group>
</Groups>
<Buttons>
<Button guid="guidEncryptConfigCommandPackageCmdSet" id="EncryptConfigCommandId" priority="0x0100" type="Button">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ButtonsGroup" />
<Icon guid="guidImages" id="bmpPic1" />
<Strings>
<ButtonText>Encrypt Mail Settings</ButtonText>
</Strings>
</Button>
<Button guid="guidEncryptConfigCommandPackageCmdSet" id="DecryptConfigCommandId" priority="0x0110" type="Button">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ButtonsGroup" />
<Icon guid="guidImages" id="bmpPic1" />
<Strings>
<ButtonText>Decrypt Mail Settings</ButtonText>
</Strings>
</Button>
</Buttons>
<!--The bitmaps section is used to define the bitmaps that are used for the commands.-->
<Bitmaps>
<Bitmap guid="guidImages" href="Resources\EncryptConfigCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>
</Bitmaps>
</Commands>
And don't forget to define the IDSymbol
in GuidSymbol
:
<!-- This is the guid used to group the menu commands together -->
<GuidSymbol name="guidEncryptConfigCommandPackageCmdSet" value="{70c1a496-88b4-4c83-8539-39decdbdb8fb}">
<IDSymbol name="ButtonsGroup" value="0x1020" />
<IDSymbol name="EncryptConfigCommandId" value="0x0100" />
<IDSymbol name="DecryptConfigCommandId" value="0x0110" />
<IDSymbol name="SubMenu1" value="0x1100"/>
<IDSymbol name="SubMenuGroup" value="0x1150"/>
</GuidSymbol>
According to these three document above:
1.We can add submenu to an existing menu or custom menu according to the first document.Not clearly described in documents how to structure the button, menu, group
and what's the relationship between them, but check the content in its code we can find 1. To add a submenu
to Tools menu
, we need to set a group as its parent, then sets the Tools menu
as its parent.
2.And to group two buttons to a submenu, we need to set the two buttons's parent as GroupB, then set the SubMenu as GroupB's parent.
3.According to third document, the menus and groups on the Visual Studio menu bar use the GUID guidSHLMainMenu. And the ID of Tools menu
is IDM_VS_MENU_TOOLS
.
That's why I edit the content in this structure, luckily it works. And since I may misunderstand something in the documents, if there's something wrong or something can be better feel free to correct me:)
The appearance when debugging:
In addition:
1.About managing the handler of the commands see this.
2.See this document which indicates to add submenu to another VS menu, we need a a group in this process. To add a group to an existing Visual Studio menu, set one of the following menus as its parent.
回答2:
You need to create Menu
for your buttons
<Menus>
<Menu guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" priority="0x0100" type="Menu">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup"/>
<Strings>
<MenuText>Config Encryptor</MenuText>
<ButtonText>Config Encryptor</ButtonText>
<CommandName>Config Encryptor</CommandName>
</Strings>
</Menu>
</Menus>
And set parents for Group
<Group guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" priority="0x0100">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu"/>
</Group>
<Group guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" priority="0x0200">
<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ToolsMenu"/>
</Group>
Also don't forget to add IDSymbol
for Group
and GroupMenu
and set correct name for Tools id="ToolsMenu"
来源:https://stackoverflow.com/questions/57146424/how-can-i-nest-tool-menu-buttons-for-a-vs-extension