Skip to main content
 首页 » 编程设计

c#之如何使用 VSPackage 在 Visual Studio 2012 中添加多个菜单项

2024年02月13日16java哥

我研究过这个link为了在 Visual Studio 2012 中使用 VSPackage 添加自定义菜单命令。

一切都很好,直到我想添加多个菜单项。

在提供的文档中,他们没有解释如何再添加一个菜单项。

例如,我在TfsUtility.vsct文件中做了如下设置:

<Menus> 
    <Menu guid="guidTfsUtilityCmdSet" id="TfsUtility" priority="0x700" type="Menu"> 
        <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS" /> 
        <Strings> 
            <ButtonText>Tfs Utility</ButtonText> 
            <CommandName>Tfs Utility</CommandName> 
        </Strings> 
    </Menu> 
</Menus> 

...

<Group guid="guidTfsUtilityCmdSet" id="MyMenuGroup" priority="0x0600"> 
    <Parent guid="guidTfsUtilityCmdSet" id="TfsUtility"/> 
</Group> 

...

<Button guid="guidTfsUtilityCmdSet" id="cmdidMyCommand" priority="0x0100" type="Button"> 
    <Parent guid="guidTfsUtilityCmdSet" id="MyMenuGroup" /> 
    <Strings> 
        <ButtonText>Branch</ButtonText> 
    </Strings> 
</Button> 

...

<GuidSymbol name="guidTfsUtilityCmdSet" value="{d5549d5d-47af-40e0-a7e5-e9ed7f64d577}"> 
 
    <IDSymbol name="MyMenuGroup" value="0x1020" /> 
    <IDSymbol name="cmdidMyCommand" value="0x0100" /> 
 
    <IDSymbol name="TfsUtility" value="0x1021"/> 
</GuidSymbol> 

问题:

  1. 如何在菜单选项中添加更多选项(例如在分支之后)?

  2. 如何为每个菜单项关联命令?我想为每个菜单项显示不同的消息。

非常感谢

请您参考如下方法:

  1. 要再添加一个菜单项,请再添加一个具有不同 ID 的 Button 部分。

  2. 要执行命令,您需要提供 MenuItemCallback 并将其注册到 OleMenuCommandService:

OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

if ( null != mcs ) 
{ 
   // Create the command for the menu item. 
   CommandID menuCommandID = new CommandID(guidCommandGroup, myCommandID); 
   MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID ); 
   mcs.AddCommand( menuItem ); 
} 

参见 Command Implementation获取更多信息。