通过命令表(Command Table) VSCT文件注册
跟菜单相关有三个概念
1) 菜单本体
在<Menu>标记中注册
一级菜单注册示例,一级菜单是和IDE的文件、编辑、工具等一级菜单并列的菜单
< Menu guid = " guidVSPackage2CmdSet "
id = " TopLevelMenu " priority = " 0x700 " type = " Menu " >
< Parent guid = " guidSHLMainMenu "
id = " IDG_VS_MM_TOOLSADDINS " />
< Strings >
< CommandName > 果冻星球 </ CommandName >
< ButtonText > 果冻星球 </ ButtonText >
< MenuText > 果冻星球 </ MenuText >
</ Strings >
</ Menu >
二级菜单示例
< Menu guid = " guidVSPackage2CmdSet "
id = " SubMenu " priority = " 0x0000 " type = " Menu " >
< Parent guid = " guidVSPackage2CmdSet "
id = " MyMenuGroup " />
< Strings >
< CommandName > 果冻工厂 </ CommandName >
< ButtonText > 果冻工厂 </ ButtonText >
< MenuText > 果冻工厂 </ MenuText >
</ Strings >
</ Menu >
关键是指定Parent是一级菜单的Group的ID,则不是一级菜单ID(见下段). 系统自动向一级菜单的Group中添加一个菜单项,而不用再到<Buttons>标记中注册二级菜单对应的根Button
2)菜单区域内的Group
每个菜单都要有一个对应的Group. (可否有多个?)
在<Groups>标记中注册
和一级菜单对应的Group,指定Parent是一级菜单ID
< Group guid = " guidVSPackage2CmdSet " id = " MyMenuGroup " priority = " 0x0600 " >
< Parent guid = " guidVSPackage2CmdSet " id = " TopLevelMenu " />
</ Group >
和二级菜单对应的Group,指定Parent是二级菜单ID
< Group guid = " guidVSPackage2CmdSet " id = " MyMenuGroupSub " priority = " 0x0601 " >
< Parent guid = " guidVSPackage2CmdSet " id = " SubMenu " />
</ Group >
3)菜单项
在<Buttons>标记中注册,指定Parent为菜单区域
< Button guid = " guidVSPackage2CmdSet " id = " cmdidMyCommand3 " priority = " 0x0100 " type = " Button " >
< Parent guid = " guidVSPackage2CmdSet " id = " MyMenuGroupSub " />
< Icon guid = " guidOfficeIcon " id = " msotcidNoIcon " />
< Strings >
< CommandName > cmdidMyCommand3 </ CommandName >
< ButtonText > 果冻仓库 </ ButtonText >
</ Strings >
</ Button >
其它注意点:
a)Menu的CommandName属性按理说可以为英文,但实际上是如果是英文的话,运行时菜单显示的文本也是英文。但Button标记(见下文)的行为就不是这样,行为不一致
b)priority指定菜单的显示优先级,小的在前显示。对二级菜单来说,如果要调根项在一级菜单中位置,要调二级菜单的Menu标记的priority属性和一级菜单的Button的priority属性
c)注意id属性都要在<Symbols>标记的<GuidSymbol>标记中注册
一个完整的示例
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< CommandTable xmlns = " http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable " xmlns:xs = " http://www.w3.org/2001/XMLSchema " >
< Extern href = " stdidcmd.h " />
< Extern href = " vsshlids.h " />
< Extern href = " msobtnid.h " />
< Extern href = " virtkeys.h " />
< Commands package = " ExamplePackageUiPackage " >
< Menus >
< Menu guid = " ExamplePackageCommandSet "
id = " TopLevelMenu " priority = " 0x700 " type = " Menu " >
< Parent guid = " guidSHLMainMenu "
id = " IDG_VS_MM_TOOLSADDINS " />
< Strings >
< CommandName > 一级菜单 </ CommandName >
< ButtonText > 一级菜单 </ ButtonText >
< MenuText > 一级菜单 </ MenuText >
</ Strings >
</ Menu >
< Menu guid = " ExamplePackageCommandSet "
id = " SubMenu " priority = " 0x0000 " type = " Menu " >
< Parent guid = " ExamplePackageCommandSet "
id = " MyMenuGroup " />
< Strings >
< CommandName > 二级菜单 </ CommandName >
< ButtonText > 二级菜单 </ ButtonText >
< MenuText > 二级菜单 </ MenuText >
</ Strings >
</ Menu >
</ Menus >
< Groups >
< Group guid = " ExamplePackageCommandSet " id = " MyMenuGroup " priority = " 0x0600 " >
< Parent guid = " ExamplePackageCommandSet " id = " TopLevelMenu " />
</ Group >
< Group guid = " ExamplePackageCommandSet " id = " MyMenuGroupSub " priority = " 0x0601 " >
< Parent guid = " ExamplePackageCommandSet " id = " SubMenu " />
</ Group >
</ Groups >
< Buttons >
< Button guid = " ExamplePackageCommandSet " id = " ShowModelExplorerCommand " priority = " 0x0000 " type = " Button " >
< Parent guid = " ExamplePackageCommandSet " id = " MyMenuGroupSub " />
< Icon guid = " guidOfficeIcon " id = " msotcidNoIcon " />
< Strings >
< CommandName > ShowModelExplorerCommand </ CommandName >
< ButtonText > 二级菜单项 </ ButtonText >
</ Strings >
</ Button >
< Button guid = " ExamplePackageCommandSet " id = " CodingAnalysisCommand " priority = " 0x0101 " type = " Button " >
< Parent guid = " ExamplePackageCommandSet " id = " MyMenuGroup " />
< Icon guid = " guidOfficeIcon " id = " msotcidNoIcon " />
< Strings >
< CommandName > CodingAnalysisCommand </ CommandName >
< ButtonText > 第二个一级菜单项 </ ButtonText >
</ Strings >
</ Button >
</ Buttons >
</ Commands >
< Symbols >
< GuidSymbol name = " ExamplePackageUiPackage " value = " {ca5700b0-91ca-4265-9d79-905695a0791c} " />
< GuidSymbol name = " ExamplePackageCommandSet " value = " {69809497-247e-40a1-a4cc-bde8b75345ef} " >
< IDSymbol name = " MyMenuGroup " value = " 0x0000 " />
< IDSymbol name = " MyMenuGroupSub " value = " 0x0001 " />
< IDSymbol name = " TopLevelMenu " value = " 0x0002 " />
< IDSymbol name = " SubMenu " value = " 0x0003 " />
< IDSymbol name = " CodingAnalysisCommand " value = " 0x0004 " />
< IDSymbol name = " ShowModelExplorerCommand " value = " 0x0005 " />
</ GuidSymbol >
</ Symbols >
</ CommandTable >
效果
===============================================================
可以转载,但不可用于赢利性用途。保留所有权利 。
===============================================================