首先我们使用向导生成一个package的扩展,里面就会发现一个vsct文件。vsct文件的全称是Visual Studio Command Table,它其实就是一个xml文件,通过一定的规则来描述visual Studio的command布局。
添加一个菜单项:
首先我们在vsct文件你们加一个菜单项:
<?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"/> <Commands package="vsPackage"> <Groups> <Group guid="cmdSet" id="MyMenuGroup" priority="0x0600"> <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/> </Group> </Groups> <Buttons> <Button guid="cmdSet" id="cmdidMyCommand" priority="0x0100" type="Button"> <Parent guid="cmdSet" id="MyMenuGroup" /> <Strings> <ButtonText>我的工具箱</ButtonText> </Strings> </Button> </Buttons> </Commands> <Symbols> <GuidSymbol name="vsPackage" value="{0b468468-bf83-4dc6-9253-ca5970142e5d}" /> <GuidSymbol name="cmdSet" value="{b001c53d-3e2d-4c8f-9787-73ed59a7c412}"> <IDSymbol name="MyMenuGroup" value="0x1020" /> <IDSymbol name="cmdidMyCommand" value="0x0100" /> </GuidSymbol> </Symbols></CommandTable>
其中高亮部分就是我添加的内容,执行效果如下:
再次添加一个菜单项:
再次添加一个菜单项也是非常简单的,只要在Buttons里面增加一项即可:
<?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"/> <Commands package="vsPackage"> <Groups> <Group guid="cmdSet" id="MyMenuGroup" priority="0x0600"> <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/> </Group> </Groups> <Buttons> <Button guid="cmdSet" id="cmdidMyCommand" priority="0x0100" type="Button"> <Parent guid="cmdSet" id="MyMenuGroup" /> <Strings> <ButtonText>我的工具箱</ButtonText> </Strings> </Button> <Button guid="cmdSet" id="cmdidMyCommand2" priority="0x0101" type="Button"> <Parent guid="cmdSet" id="MyMenuGroup" /> <Strings> <ButtonText>我的工具箱2</ButtonText> </Strings> </Button> </Buttons> </Commands> <Symbols> <GuidSymbol name="vsPackage" value="{0b468468-bf83-4dc6-9253-ca5970142e5d}" /> <GuidSymbol name="cmdSet" value="{b001c53d-3e2d-4c8f-9787-73ed59a7c412}"> <IDSymbol name="MyMenuGroup" value="0x1020" /> <IDSymbol name="cmdidMyCommand" value="0x0100" /> <IDSymbol name="cmdidMyCommand2" value="0x0101" /> </GuidSymbol> </Symbols></CommandTable>
生成的效果如下:
添加分组
现在我们来把菜单进行分组:首先在Groups项中添加一个Group,然后修改Button的ParentId即可。
<?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"/> <Commands package="vsPackage"> <Groups> <Group guid="cmdSet" id="MyMenuGroup" priority="0x0600"> <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/> </Group> <Group guid="cmdSet" id="MyMenuGroup2" priority="0x0601"> <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/> </Group> </Groups> <Buttons> <Button guid="cmdSet" id="cmdidMyCommand" priority="0x0100" type="Button"> <Parent guid="cmdSet" id="MyMenuGroup" /> <Strings> <ButtonText>我的工具箱</ButtonText> </Strings> </Button> <Button guid="cmdSet" id="cmdidMyCommand2" priority="0x0101" type="Button"> <Parent guid="cmdSet" id="MyMenuGroup2" /> <Strings> <ButtonText>我的工具箱2</ButtonText> </Strings> </Button> </Buttons> </Commands> <Symbols> <GuidSymbol name="vsPackage" value="{0b468468-bf83-4dc6-9253-ca5970142e5d}" /> <GuidSymbol name="cmdSet" value="{b001c53d-3e2d-4c8f-9787-73ed59a7c412}"> <IDSymbol name="MyMenuGroup" value="0x1020" /> <IDSymbol name="MyMenuGroup2" value="0x1021" /> <IDSymbol name="cmdidMyCommand" value="0x0100" /> <IDSymbol name="cmdidMyCommand2" value="0x0101" /> </GuidSymbol> </Symbols></CommandTable>
效果如下:
在菜单中添加图标
在菜单中添加图标要稍微麻烦点:首先需要添加一个Bitmaps分组,然后修改Button的Icon属性。
<?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"/> <Commands package="vsPackage"> <Groups> <Group guid="cmdSet" id="MyMenuGroup" priority="0x0600"> <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/> </Group> <Group guid="cmdSet" id="MyMenuGroup2" priority="0x0601"> <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/> </Group> </Groups> <Buttons> <Button guid="cmdSet" id="cmdidMyCommand" priority="0x0100" type="Button"> <Parent guid="cmdSet" id="MyMenuGroup" /> <Icon guid="guidImages" id="bmpPic1" /> <Strings> <ButtonText>我的工具箱</ButtonText> </Strings> </Button> <Button guid="cmdSet" id="cmdidMyCommand2" priority="0x0101" type="Button"> <Parent guid="cmdSet" id="MyMenuGroup2" /> <Icon guid="guidImages" id="bmpPicSearch" /> <Strings> <ButtonText>我的工具箱2</ButtonText> </Strings> </Button> </Buttons> <Bitmaps> <Bitmap guid="guidImages" href="Resources\Images.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows"/> </Bitmaps> </Commands> <Symbols> <GuidSymbol name="vsPackage" value="{0b468468-bf83-4dc6-9253-ca5970142e5d}" /> <GuidSymbol name="cmdSet" value="{b001c53d-3e2d-4c8f-9787-73ed59a7c412}"> <IDSymbol name="MyMenuGroup" value="0x1020" /> <IDSymbol name="MyMenuGroup2" value="0x1021" /> <IDSymbol name="cmdidMyCommand" value="0x0100" /> <IDSymbol name="cmdidMyCommand2" value="0x0101" /> </GuidSymbol> <GuidSymbol name="guidImages" value="{f3517729-ad9a-4c24-9f72-288fafa0758d}" > <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></CommandTable>
生成的效果如下:
在菜单中关联命令
通过向导生成Package时,会自动生成一个Package的子类,重载其Initialize函数,在里面加入事件处理函数即可。向导默认已经给我们生成了一个示例,我们只需要修改其处理函数和添加新的按钮的事件即可。
protected override void Initialize() { base.Initialize(); // Add our command handlers for menu (commands must exist in the .vsct file) OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if (null != mcs) { // Create the command for the menu item. CommandID menuCommandID = new CommandID(GuidList.guidVSPackage3CmdSet, (int)PkgCmdIDList.cmdidMyCommand); MenuCommand menuItem = new MenuCommand((s, e) => System.Windows.Forms.MessageBox.Show("hello world"), menuCommandID); mcs.AddCommand(menuItem); } }