博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Visual Studio Package扩展——vsct文件简介
阅读量:5895 次
发布时间:2019-06-19

本文共 6291 字,大约阅读时间需要 20 分钟。

首先我们使用向导生成一个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);
        }
    }

转载地址:http://pbxsx.baihongyu.com/

你可能感兴趣的文章
解除 Linux 系统的最大进程数和最大文件打开数限制
查看>>
使用优盘或者移动硬盘安装Ubuntu
查看>>
electron-创建一个hello world应用
查看>>
RXjs相关
查看>>
百练2973: Skew binary 数 之 Java 题解
查看>>
SaltStack配置管理
查看>>
linux基础命令 head
查看>>
在模板中将php数组转换成js对象
查看>>
使用java调用FFMPEG进行转码
查看>>
克隆虚拟机以及互相登陆
查看>>
AES加密解密
查看>>
酷客多小程序会员体系上线,你不可不知道!
查看>>
objective c:import和include的区别, ""和<>区别
查看>>
CentOS 6.5上部署drbd
查看>>
spring SchedulerFactoryBean 没有创建 Scheduler的实现类bea
查看>>
基于cobbler实现自动化安装系统
查看>>
java基础专栏—IOUtils(4)
查看>>
TimeUnit使用
查看>>
VMware VIX API使用教程
查看>>
The Shared folder with you
查看>>