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

[ay vs2015 sdk]开发笔记[3]-多级菜单,动态菜单与命令

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

(AY)动态菜单列表从开始菜单上的占位符。 每次显示的菜单,Visual Studio 集成的开发环境 (IDE) 要求的所有命令都应显示在占位符 VSPackage。 动态列表会在菜单上的任意位置。 但是,动态列表将通常存储,并显示由其自己,位于子菜单或菜单的底部。 通过使用这些设计模式,可以使命令进行扩展和收缩而不会影响菜单上的其他命令的位置的动态列表。 在本演练中,动态 MRU 列表显示在现有子菜单,子菜单中的其余部分分开是一条线的底部。 从技术上讲,动态列表也应用到工具栏。 但是,我们不鼓励使用情况因为工具栏应保持不变,除非用户执行特定步骤来更改它。 本演练中创建的每次选择其中一个更改其顺序的四个项目的 MRU 列表 (选定的项目移动到列表的顶部)。

<IDSymbol name="MRUListGroup" value="0x1200"/>
<IDSymbol name="cmdidMRUList" value="0x0200"/>

blob.png

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


在现有的按钮下面加一个按钮

      <Button guid="guidAyCommandPackageCmdSet" id="cmdidMRUList" type="Button" priority="0x0100">
        <Parent guid="guidAyCommandPackageCmdSet" id="MRUListGroup" />
        <CommandFlag>DynamicItemStart</CommandFlag>
        <Strings>
          <CommandName>cmdidMRUList</CommandName>
          <ButtonText>MRU Placeholder</ButtonText>
        </Strings>
      </Button>

DynamicItemStart 标记代表这个命令是动态增加的

单击TestMenu,然后Sub Menu,展示新命令MRU Placeholder,关于动态命令列表的生成 ,下面我们来写。



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



添加一个静态的菜单,复习上节课,只需要改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">


  <!--  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="guidAyCommandPackage">
    <!-- 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. -->


    <Menus>

      <Menu guid="guidAyCommandPackageCmdSet" id="TopLevelMenu" priority="0x700" type="Menu">
        <Parent guid="guidSHLMainMenu"
                id="IDG_VS_MM_TOOLSADDINS" />
        <Strings>
          <ButtonText>TestMenu</ButtonText>
          <CommandName>TestMenu</CommandName>
        </Strings>
      </Menu>
      <Menu guid="guidAyCommandPackageCmdSet" id="SubMenu" priority="0x0100" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="MyMenuGroup"/>
        <Strings>
          <ButtonText>Sub Menu</ButtonText>
          <CommandName>Sub Menu</CommandName>
        </Strings>
      </Menu>
      <Menu guid="guidAyCommandPackageCmdSet" id="DynamicMenu" priority="0x0101" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="MyMenuGroup"/>
        <Strings>
          <ButtonText>ay动态菜单1</ButtonText>
          <CommandName>DynamicMenu</CommandName>
        </Strings>
      </Menu>
      <Menu guid="guidAyCommandPackageCmdSet" id="DynamicMenu2" priority="0x0102" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="MyMenuGroup"/>
        <Strings>
          <ButtonText>ay动态菜单2(真)</ButtonText>
          <CommandName>DynamicMenu2</CommandName>
        </Strings>
      </Menu>
    </Menus>

    <!-- 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="guidAyCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidAyCommandPackageCmdSet" id="TopLevelMenu"/>
      </Group>
      <Group guid="guidAyCommandPackageCmdSet" id="MRUListFirstGroup" priority="0x0611">
        <Parent guid="guidAyCommandPackageCmdSet" id="TopLevelMenu"/>
      </Group>
      <Group guid="guidAyCommandPackageCmdSet" id="SubMenuGroup" priority="0x0100">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenu"/>
      </Group>
      
      <Group guid="guidAyCommandPackageCmdSet" id="SubMenuGroup1" priority="0x0101">
        <Parent guid="guidAyCommandPackageCmdSet" id="DynamicMenu2"/>
      </Group>
      
      
      <!--<Group guid="guidAyCommandPackageCmdSet" id="MRUListGroup" priority="0x0602">
        <Parent guid="guidAyCommandPackageCmdSet" id="DynamicMenu"/>
      </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="guidAyCommandPackageCmdSet" id="AyCommandId" priority="0x0100" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <CommandName>AyCommandId</CommandName>
          <ButtonText>开始 AyCommand5</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidAyCommandPackageCmdSet" id="cmdidTestSubCommand" priority="0x0000" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidTestSubCommand</CommandName>
          <ButtonText>Test Sub Command</ButtonText>
        </Strings>
      </Button>
      <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand" priority="0x0001" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand</CommandName>
          <ButtonText>打开记事本</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand1" priority="0x0002" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup1" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand1</CommandName>
          <ButtonText>打开记事本1</ButtonText>
        </Strings>
      </Button>
      <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand2" priority="0x0003" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup1" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand2</CommandName>
          <ButtonText>打开记事本2</ButtonText>
        </Strings>
      </Button>

      <!--<Button guid="guidAyCommandPackageCmdSet" id="cmdidMRUList" type="Button" priority="0x0100">
        <Parent guid="guidAyCommandPackageCmdSet" id="MRUListGroup" />
        <CommandFlag>DynamicItemStart</CommandFlag>
        <Strings>
          <CommandName>cmdidMRUList</CommandName>
          <ButtonText>MRU Placeholder</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\AyCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>
    </Bitmaps>
  </Commands>

  <KeyBindings>
    <KeyBinding guid="guidAyCommandPackageCmdSet" id="AyCommandId" editor="guidVSStd97" key1="4"  mod1="CONTROL" mod2="CONTROL" key2="VK_F1"/>
    <KeyBinding guid="guidAyCommandPackageCmdSet" id="cmdidTestSubCommand" editor="guidVSStd97" key1="2"  mod1="CONTROL" />
    <KeyBinding guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand" editor="guidVSStd97" key1="J"  mod1="CONTROL" mod2="CONTROL" key2="S"/>
  </KeyBindings>


  <Symbols>
    <!-- This is the package guid. -->
    <GuidSymbol name="guidAyCommandPackage" value="{75a23647-9848-418c-8800-6a144465d398}" >
    </GuidSymbol>

    <!-- This is the guid used to group the menu commands together -->
    <GuidSymbol name="guidAyCommandPackageCmdSet" value="{206b5d97-fba4-4223-9342-08cc19cd3a19}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="AyCommandId" value="0x0100" />
      <IDSymbol name="TopLevelMenu" value="0x1021" />

      <IDSymbol name="SubMenu" value="0x1100"/>
      <IDSymbol name="SubMenuGroup" value="0x1150"/>
      
      <IDSymbol name="SubMenu1" value="0x1101"/>
      <IDSymbol name="SubMenuGroup1" value="0x1151"/>
      
      <IDSymbol name="cmdidTestSubCommand" value="0x0105"/>
      <IDSymbol name="cmdidNotepadCommand" value="0x0106"/>

      <IDSymbol name="cmdidNotepadCommand1" value="0x0107"/>
      <IDSymbol name="cmdidNotepadCommand2" value="0x0108"/>
      
      <IDSymbol name="DynamicMenu" value="0x1199"/>
      <IDSymbol name="DynamicMenu2" value="0x1120"/>
      
      <IDSymbol name="MRUListFirstGroup" value="0x1121"/>
      <IDSymbol name="MRUListGroup" value="0x1200"/>
      <IDSymbol name="cmdidMRUList" value="0x0200"/>
    </GuidSymbol>

    <GuidSymbol name="guidImages" value="{65c53e13-8b40-4219-a89f-71b1b10b97d3}" >
      <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>

总结:如果Menu下没有子菜单或者命令,那么这个父级菜单是不会显示的

blob.png

增加DynamicMenu组,出现2个杠的隔开的样式的,默认单个在一组的是没有杠的。

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="guidAyCommandPackage">
    <!-- 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. -->


    <Menus>

      <Menu guid="guidAyCommandPackageCmdSet" id="TopLevelMenu" priority="0x700" type="Menu">
        <Parent guid="guidSHLMainMenu"
                id="IDG_VS_MM_TOOLSADDINS" />
        <Strings>
          <ButtonText>TestMenu</ButtonText>
          <CommandName>TestMenu</CommandName>
        </Strings>
      </Menu>
      <Menu guid="guidAyCommandPackageCmdSet" id="SubMenu" priority="0x0100" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="MyMenuGroup"/>
        <Strings>
          <ButtonText>Sub Menu</ButtonText>
          <CommandName>Sub Menu</CommandName>
        </Strings>
      </Menu>
      <Menu guid="guidAyCommandPackageCmdSet" id="DynamicMenu1" priority="0x0101" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="MRUListFirstGroup"/>
        <Strings>
          <ButtonText>ay动态菜单1(静态)</ButtonText>
          <CommandName>DynamicMenu</CommandName>
        </Strings>
      </Menu>
      <Menu guid="guidAyCommandPackageCmdSet" id="DynamicMenu2" priority="0x0102" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="MRUListFirstGroup"/>
        <Strings>
          <ButtonText>ay动态菜单2(静态)</ButtonText>
          <CommandName>DynamicMenu2</CommandName>
        </Strings>
      </Menu>
    </Menus>

    <!-- 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="guidAyCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidAyCommandPackageCmdSet" id="TopLevelMenu"/>
      </Group>
      <Group guid="guidAyCommandPackageCmdSet" id="MRUListFirstGroup" priority="0x0611">
        <Parent guid="guidAyCommandPackageCmdSet" id="TopLevelMenu"/>
      </Group>
      <Group guid="guidAyCommandPackageCmdSet" id="SubMenuGroup" priority="0x0100">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenu"/>
      </Group>
      
      <Group guid="guidAyCommandPackageCmdSet" id="SubMenuGroup1" priority="0x0101">
        <Parent guid="guidAyCommandPackageCmdSet" id="DynamicMenu2"/>
      </Group>
      <Group guid="guidAyCommandPackageCmdSet" id="SubMenuGroup2" priority="0x0101">
        <Parent guid="guidAyCommandPackageCmdSet" id="DynamicMenu1"/>
      </Group>
      
      <!--<Group guid="guidAyCommandPackageCmdSet" id="MRUListGroup" priority="0x0602">
        <Parent guid="guidAyCommandPackageCmdSet" id="DynamicMenu"/>
      </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="guidAyCommandPackageCmdSet" id="AyCommandId" priority="0x0100" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <CommandName>AyCommandId</CommandName>
          <ButtonText>开始 AyCommand5</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidAyCommandPackageCmdSet" id="cmdidTestSubCommand" priority="0x0000" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidTestSubCommand</CommandName>
          <ButtonText>Test Sub Command</ButtonText>
        </Strings>
      </Button>
      <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand" priority="0x0001" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand</CommandName>
          <ButtonText>打开记事本</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand1" priority="0x0002" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup1" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand1</CommandName>
          <ButtonText>打开记事本1</ButtonText>
        </Strings>
      </Button>
      <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand2" priority="0x0003" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup1" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand2</CommandName>
          <ButtonText>打开记事本2</ButtonText>
        </Strings>
      </Button>
      <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand3" priority="0x0004" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup2" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand3</CommandName>
          <ButtonText>打开记事本3</ButtonText>
        </Strings>
      </Button>
      
      <!--<Button guid="guidAyCommandPackageCmdSet" id="cmdidMRUList" type="Button" priority="0x0100">
        <Parent guid="guidAyCommandPackageCmdSet" id="MRUListGroup" />
        <CommandFlag>DynamicItemStart</CommandFlag>
        <Strings>
          <CommandName>cmdidMRUList</CommandName>
          <ButtonText>MRU Placeholder</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\AyCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>
    </Bitmaps>
  </Commands>

  <KeyBindings>
    <KeyBinding guid="guidAyCommandPackageCmdSet" id="AyCommandId" editor="guidVSStd97" key1="4"  mod1="CONTROL" mod2="CONTROL" key2="VK_F1"/>
    <KeyBinding guid="guidAyCommandPackageCmdSet" id="cmdidTestSubCommand" editor="guidVSStd97" key1="2"  mod1="CONTROL" />
    <KeyBinding guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand" editor="guidVSStd97" key1="J"  mod1="CONTROL" mod2="CONTROL" key2="S"/>
  </KeyBindings>


  <Symbols>
    <!-- This is the package guid. -->
    <GuidSymbol name="guidAyCommandPackage" value="{75a23647-9848-418c-8800-6a144465d398}" >
    </GuidSymbol>

    <!-- This is the guid used to group the menu commands together -->
    <GuidSymbol name="guidAyCommandPackageCmdSet" value="{206b5d97-fba4-4223-9342-08cc19cd3a19}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="AyCommandId" value="0x0100" />
      <IDSymbol name="TopLevelMenu" value="0x1021" />

      <IDSymbol name="SubMenu" value="0x1100"/>
      <IDSymbol name="SubMenuGroup" value="0x1150"/>
      
      <IDSymbol name="SubMenu1" value="0x1101"/>
      <IDSymbol name="SubMenuGroup1" value="0x1151"/>
      <IDSymbol name="SubMenuGroup2" value="0x1152"/>
      
      <IDSymbol name="cmdidTestSubCommand" value="0x0105"/>
      <IDSymbol name="cmdidNotepadCommand" value="0x0106"/>

      <IDSymbol name="cmdidNotepadCommand1" value="0x0107"/>
      <IDSymbol name="cmdidNotepadCommand2" value="0x0108"/>
      <IDSymbol name="cmdidNotepadCommand3" value="0x0109"/>
      
      <IDSymbol name="DynamicMenu" value="0x1199"/>
      <IDSymbol name="DynamicMenu1" value="0x1122"/>
      <IDSymbol name="DynamicMenu2" value="0x1120"/>
      
      <IDSymbol name="MRUListFirstGroup" value="0x1121"/>
      <IDSymbol name="MRUListGroup" value="0x1200"/>
      <IDSymbol name="cmdidMRUList" value="0x0200"/>
    </GuidSymbol>

    <GuidSymbol name="guidImages" value="{65c53e13-8b40-4219-a89f-71b1b10b97d3}" >
      <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>

想办法,三级菜单(预示着多级菜单的思路)

添加一个二级菜单栏里面的  子项 容器

  <Group guid="guidAyCommandPackageCmdSet" id="SubMenuGroup2_1" priority="0x0103">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubDynamicMenu2_1"/>
      </Group>

一个容器肯定有个归属

    <Menu guid="guidAyCommandPackageCmdSet" id="SubDynamicMenu2_1" priority="0x0103" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup1"/>
        <Strings>
          <ButtonText>子级菜单2_1</ButtonText>
          <CommandName>SubDynamicMenu2_1</CommandName>
        </Strings>
      </Menu>

增加组合菜单的id

     <IDSymbol name="SubMenuGroup2_1" value="0x11521"/>
      <IDSymbol name="SubDynamicMenu2_1" value="0x11201"/>

接下来容器里面,加上按钮,才能真正显示,一个不含有button的menu是不会显示的。

   <Button guid="guidAyCommandPackageCmdSet" id="cmdidNotepadCommand2_1" priority="0x0005" type="Button">
        <Parent guid="guidAyCommandPackageCmdSet" id="SubMenuGroup2_1" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidNotepadCommand3</CommandName>
          <ButtonText>三级菜单ay-记事本</ButtonText>
        </Strings>
      </Button>

blob.png

总结:每个Menu都是容器,也就是Group,通过Group表现层级关系,group中加上button

Group和Menu是互相依赖的,而Button只是依赖Group

这一块很复杂的。





如何动态增加Menu和命令(www.ayjs.net)

为了更清晰地演示菜单,增加一个新菜单

写一个菜单和容器,我打算在根节点下新增

 <Menu guid="guidAyCommandPackageCmdSet" id="DynamicMenu" priority="0x0104" type="Menu">
        <Parent guid="guidAyCommandPackageCmdSet" id="MyMenuGroup"/>
        <Strings>
          <ButtonText>ay动态菜单[动态]</ButtonText>
          <CommandName>DynamicMenu</CommandName>
        </Strings>
      </Menu>

此时界面根节点下,有个ay动态menu,但是没有子节点,所以不显示。

我们把menu作为容器,在他下方增加命令或者menu,所以需要容器,就是Group

每个group都需要取id,然后group需要指示是那个menu的内容的容器

   <Group guid="guidAyCommandPackageCmdSet" id="MRUListGroup" priority="0x0602">
        <Parent guid="guidAyCommandPackageCmdSet" id="DynamicMenu"/>
      </Group>
   <IDSymbol name="MRUListGroup" value="0x1200"/>
       <IDSymbol name="DynamicMenu" value="0x1199"/>
      <IDSymbol name="cmdidMRUList" value="0x0200"/>

cmdidMRUList是个命令列表,动态添加的。

DynamicItemStart标记,这一个地方的命令是动态增加的。

 <Button guid="guidAyCommandPackageCmdSet" id="cmdidMRUList" type="Button" priority="0x0200">
        <Parent guid="guidAyCommandPackageCmdSet" id="MRUListGroup" />
        <CommandFlag>DynamicItemStart</CommandFlag>
        <Strings>
          <CommandName>cmdidMRUList</CommandName>
          <ButtonText></ButtonText>
        </Strings>
      </Button>


我们打开AyCommand.cs文件,我们需要在cmdidMRUList动态添加4个子项

public const uint cmdidMRUList = 0x0200;
   private int numMRUItems = 4;
        private int baseMRUID = (int)cmdidMRUList;
        private ArrayList mruList;

        private void InitializeMRUList()
        {
            if (null == this.mruList)
            {
                this.mruList = new ArrayList();
                if (null != this.mruList)
                {
                    for (int i = 0; i < this.numMRUItems; i++)
                    {
                        this.mruList.Add(string.Format(CultureInfo.CurrentCulture, "Item {0}", i + 1));
                    }
                }
            }
        }



        private void InitMRUMenu(OleMenuCommandService mcs)
        {
            InitializeMRUList();
            for (int i = 0; i < this.numMRUItems; i++)
            {
                var cmdID = new CommandID(CommandSet, this.baseMRUID + i);
                var mc = new OleMenuCommand(new EventHandler(OnMRUExec), cmdID);
                mc.BeforeQueryStatus += new EventHandler(OnMRUQueryStatus);
                mcs.AddCommand(mc);
            }
        }
        private void OnMRUExec(object sender, EventArgs e)
        {
            var menuCommand = sender as OleMenuCommand;
            if (null != menuCommand)
            {
                int MRUItemIndex = menuCommand.CommandID.ID - this.baseMRUID;
                if (MRUItemIndex >= 0 && MRUItemIndex < this.mruList.Count)
                {
                    string selection = this.mruList[MRUItemIndex] as string;
                    for (int i = MRUItemIndex; i > 0; i--)
                    {
                        this.mruList[i] = this.mruList[i - 1];
                    }
                    this.mruList[0] = selection;
                    System.Windows.Forms.MessageBox.Show(
                        string.Format(CultureInfo.CurrentCulture,
                                      "Selected {0}", selection));
                }
            }
        }


        private void OnMRUQueryStatus(object sender, EventArgs e)
        {
            OleMenuCommand menuCommand = sender as OleMenuCommand;
            if (null != menuCommand)
            {
                int MRUItemIndex = menuCommand.CommandID.ID - this.baseMRUID;
                if (MRUItemIndex >= 0 && MRUItemIndex < this.mruList.Count)
                {
                    menuCommand.Text = this.mruList[MRUItemIndex] as string;
                }
            }
        }

然后我们在构造函数中加上

 this.InitMRUMenu(commandService);



效果图:单击的时候才加载 菜单,每次单击子项,会排序在上方

4.gif


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

总结:OleMenuCommand有两个事件,单击命令时候才初始化AyCommand.cs

先执行OleMenuCommand后面的invokeHandler

执行完之后,再执行BeforeQueryStatus

我们使用BeforeQueryStatus这个事件,来改变单击后的顺序。



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



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

猜你喜欢

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

发表评论

必填

选填

选填

必填

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

  查看权限

抖音:wpfui 工作wpf,兴趣学习flutter

目前在合肥市某公司上班,已经厌弃,如果你的公司看的上我,加我QQ私聊

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

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

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

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

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

查看捐赠

AYUI7.X MVC教程 更新如下:

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

标签列表