当前位置:网站首页 / vs2015 / 正文

[ay vs2015 sdk]开发笔记[4]-创建可重用的按钮 与 图标 与 文本

时间:2016年07月13日 | 作者 : aaronyang | 分类 : vs2015 | 浏览: 2075次 | 评论 0

如何命令的项目进行分组,以便它们可以包含多个菜单上

新建一个ReusableButtons的vsix项目

新建一个ReusableButtons的自定义命令项目

默认情况下,该命令项目模板将创建一个名为组 MyGroup 和一个按钮,已提供,以及为每个 IDSymbol 条目的名称。

我们额外加一个SecondReusableCommandId

  <GuidSymbol name="guidReusableCommandPackageCmdSet" value="{0081fec9-ef56-4dfa-8431-49ecc9f26a23}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="ReusableCommandId" value="0x0100" />

      <IDSymbol name="SecondReusableCommandId" value="0x0200" />
      
    </GuidSymbol>

在Groups部分中,创建一个具有与符号部分中提供的那些相同的 GUID 和 ID 属性的group元素。 

您还可以使用现有的组,或使用提供的命令模板,如以下示例所示的条目。 

此组将出现在 工具  (Tools)菜单下

默认

 <Groups>
 
      <Group guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
      </Group>
    </Groups>

若要创建一组以供重复使用的按钮

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

默认的Buttons下有个Button

      <Button guid="guidReusableCommandPackageCmdSet" id="ReusableCommandId" priority="0x0100" type="Button">
        <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Invoke ReusableCommand</ButtonText>
        </Strings>
      </Button>

如果Button可以用在很多地方,不止当前组,你就可以创建一个条目,指定guid和id,此条目放在CommandPlacements节点下。此节点必须在</Commands>下面增加。

  <CommandPlacements>
    <CommandPlacement guid="guidReusableCommandPackageCmdSet" id="SecondReusableCommandId" priority="0x105">
      <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
    </CommandPlacement>
  </CommandPlacements>

blob.png

下面的示例将组放 解决方案资源管理器的工具栏上,其他按钮的右侧。

  <CommandPlacements>
    <CommandPlacement guid="guidReusableCommandPackageCmdSet" id="SecondReusableCommandId" priority="0x105">
      <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
    </CommandPlacement>

      <CommandPlacement guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" priority="0xF00">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_TOOL_PROJWIN"/>
      </CommandPlacement>
    <CommandPlacement guid="guidButtonGroupCmdSet" id="MyMenuGroup" priority="0x605">
      <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
    </CommandPlacement>
  </CommandPlacements>

默认组

<Button guid="guidReusableCommandPackageCmdSet" id="ReusableCommandId" priority="0x0100" type="Button">
        <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Invoke ReusableCommand</ButtonText>
        </Strings>
      </Button>

指定了Parent是MyMenuGroup组中

blob.png

而MyMenuGroup

  <Groups>

      <Group guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
      </Group>
    </Groups>

指定的是菜单栏的工具那个menu,英文的vs是Tools菜单

所以如上出现了个菜单命令



第一个的意思是,可以把SecondReusableCommandId的组加入MyMenuGroup组中。

    <CommandPlacement guid="guidReusableCommandPackageCmdSet" id="SecondReusableCommandId" priority="0x105">
      <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
    </CommandPlacement>



第二个CommandPlacement的作用,把MyMenuGroup组放到了 下面的Parent去了

blob.png


第三句暂时没用,意思是可以把其他的Command包嵌入到指定的组,可以是自定义的,也可以是系统内置的。

<CommandPlacement guid="guidButtonGroupCmdSet" id="MyMenuGroup" priority="0x605">
      <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
    </CommandPlacement>



====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========


给命令加图标

准备个ico,8位深度,最好32位深度的。

新加一个标识

 <GuidSymbol name="testIcon" value="{6e43f53c-4759-4197-a960-9781ee867e04}">
      
    </GuidSymbol>

blob.png

右键Resources文件夹添加ico文件,ico最好16*16

blob.png

找到Bitmaps节点

blob.png

<?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">

  <!--  This is the file that defines the actual layout and type of the commands.
        It is divided in different sections (e.g. command definition, command
        placement, ...), with each defining a specific set of properties.
        See the comment before each section for more details about how to
        use it. -->

  <!--  The VSCT compiler (the tool that translates this file into the binary
        format that VisualStudio will consume) has the ability to run a preprocessor
        on the vsct file; this preprocessor is (usually) the C++ preprocessor, so
        it is possible to define includes and macros with the same syntax used
        in C++ files. Using this ability of the compiler here, we include some files
        defining some of the constants that we will use inside the file. -->

  <!--This is the file that defines the IDs for all the commands exposed by VisualStudio. -->
  <Extern href="stdidcmd.h"/>

  <!--This header contains the command ids for the menus provided by the shell. -->
  <Extern href="vsshlids.h"/>

  <!--The Commands section is where commands, menus, and menu groups are defined.
      This section uses a Guid to identify the package that provides the command defined inside it. -->
  <Commands package="guidReusableCommandPackage">
    <!-- Inside this section we have different sub-sections: one for the menus, another
    for the menu groups, one for the buttons (the actual commands), one for the combos
    and the last one for the bitmaps used. Each element is identified by a command id that
    is a unique pair of guid and numeric identifier; the guid part of the identifier is usually
    called "command set" and is used to group different command inside a logically related
    group; your package should define its own command set in order to avoid collisions
    with command ids defined by other packages. -->

    <!-- In this section you can define new menu groups. A menu group is a container for
         other menus or buttons (commands); from a visual point of view you can see the
         group as the part of a menu contained between two lines. The parent of a group
         must be a menu. -->
    <Groups>

      <Group guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
      </Group>
    </Groups>

    <!--Buttons section. -->
    <!--This section defines the elements the user can interact with, like a menu command or a button
        or combo box in a toolbar. -->
    <Buttons>
      <!--To define a menu group you have to specify its ID, the parent menu and its display priority.
          The command is visible and enabled by default. If you need to change the visibility, status, etc, you can use
          the CommandFlag node.
          You can add more than one CommandFlag node e.g.:
              <CommandFlag>DefaultInvisible</CommandFlag>
              <CommandFlag>DynamicVisibility</CommandFlag>
          If you do not want an image next to your command, remove the Icon node /> -->
      <Button guid="guidReusableCommandPackageCmdSet" id="ReusableCommandId" priority="0x0100" type="Button">
        <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Invoke ReusableCommand</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidReusableCommandPackageCmdSet" id="cmdidMyCommand" priority="0x0101" type="Button">
        <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="testIcon" id="testIcon1" />
        <Strings>
          <ButtonText>ay测试图标命令</ButtonText>
        </Strings>
      </Button>


    </Buttons>

    <!--The bitmaps section is used to define the bitmaps that are used for the commands.-->
    <Bitmaps>
      <!--  The bitmap id is defined in a way that is a little bit different from the others:
            the declaration starts with a guid for the bitmap strip, then there is the resource id of the
            bitmap strip containing the bitmaps and then there are the numeric ids of the elements used
            inside a button definition. An important aspect of this declaration is that the element id
            must be the actual index (1-based) of the bitmap inside the bitmap strip. -->
      <Bitmap guid="guidImages" href="Resources\ReusableCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>
      <Bitmap guid="testIcon" href="Resources\ayui_48.ico"  usedList="testIcon1"/>


    </Bitmaps>
  </Commands>

  <CommandPlacements>
    <CommandPlacement guid="guidReusableCommandPackageCmdSet" id="SecondReusableCommandId" priority="0x105">
      <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
    </CommandPlacement>

    <CommandPlacement guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" priority="0xF00">
      <Parent guid="guidSHLMainMenu" id="IDM_VS_TOOL_PROJWIN"/>
    </CommandPlacement>
    <!--<CommandPlacement guid="guidButtonGroupCmdSet" id="MyMenuGroup" priority="0x605">
      <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
    </CommandPlacement>-->
  </CommandPlacements>

  <Symbols>
    <!-- This is the package guid. -->
    <GuidSymbol name="guidReusableCommandPackage" value="{7bc97c0d-f767-484c-a681-2025ceea8657}" />

    <!-- This is the guid used to group the menu commands together -->
    <GuidSymbol name="guidReusableCommandPackageCmdSet" value="{0081fec9-ef56-4dfa-8431-49ecc9f26a23}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="ReusableCommandId" value="0x0100" />

      <IDSymbol name="SecondReusableCommandId" value="0x0200" />
      <IDSymbol name="cmdidMyCommand" value="0x0201" />

    </GuidSymbol>

    <GuidSymbol name="guidImages" value="{f7737e65-809a-4a7f-89e5-cfb2dd93481d}" >
      <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>

    <GuidSymbol name="testIcon" value="{6e43f53c-4759-4197-a960-9781ee867e04}">
      <IDSymbol name="testIcon1" value="1" />
    </GuidSymbol>
  </Symbols>
</CommandTable>

运行项目,由于图标太大了,所以建议是16*16的图标

blob.png

blob.png

icon和png互转工具:前往下载

icon尺寸在线调整:立即前往


重新添加图片

添加图片

blob.png

xml中,重新指定bitmap的资源名字。保持一致。


blob.png

blob.png





更改菜单命令的文本

     <Button guid="guidReusableCommandPackageCmdSet" id="ChangeMenuTextId" priority="0x0102" type="Button">
        <Parent guid="guidReusableCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <CommandFlag>TextChanges</CommandFlag>
        <Strings>
          <ButtonText>Invoke ChangeMenuText</ButtonText>
        </Strings>
      </Button>

CommandTag:     这里使用TextChanges

效果就是,单击菜单,会更换菜单的文本,下方增加一个id

 <GuidSymbol name="guidReusableCommandPackageCmdSet" value="{0081fec9-ef56-4dfa-8431-49ecc9f26a23}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="ReusableCommandId" value="0x0100" />

      <IDSymbol name="SecondReusableCommandId" value="0x0200" />
      <IDSymbol name="cmdidMyCommand" value="0x0201" />

      <IDSymbol name="ChangeMenuTextId" value="0x0202" />
      
    </GuidSymbol>

然后对应的Command文件增加

 public const int ChangeMenuTextId = 0x0202;

构造函数绑定实现

  private ReusableCommand(Package package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            this.package = package;

            OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (commandService != null)
            {
                var menuCommandID = new CommandID(CommandSet, CommandId);
                var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
                commandService.AddCommand(menuItem);


                CommandID menuCommandID2 = new CommandID(CommandSet, ChangeMenuTextId);
                OleMenuCommand menuItem2 = new OleMenuCommand(new EventHandler(ShowText), menuCommandID2);
                menuItem2.BeforeQueryStatus +=
                    new EventHandler(OnBeforeQueryStatus);
                commandService.AddCommand(menuItem2);



            }
        }
  private void ShowText(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("ay自动换文本");
        }

        private void OnBeforeQueryStatus(object sender, EventArgs e)
        {
            var myCommand = sender as OleMenuCommand;
            if (null != myCommand)
            {
                myCommand.Text = "New Text";
            }
        }


效果图:单机完,菜单文本变成 New Text

3F.gif




====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========



推荐您阅读更多有关于“vs2015vsix开发,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf,目前主maui

招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰

目前在合肥市企迈科技就职

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表