Exemplo n.º 1
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initilaization code that rely on services provided by VisualStudio.
        /// </summary>
        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 tool window
                CommandID   toolwndCommandID = new CommandID(GuidList.guidDynamicStartItemExamplePackageCmdSet, PkgCmdIDList.cmdidShowDynamicItemCountEntryWindow);
                MenuCommand menuToolWin      = new MenuCommand(ShowToolWindow, toolwndCommandID);
                mcs.AddCommand(menuToolWin);

                //Add a 'dummy' entry for our menu, this entry has a null EventHandler as it will never be 'executed'
                //(you can't 'execute' a menu), and it doesn't set any properties as the default value of Visible is
                //true and we only want this handler in place so that the command system will find it once our package
                //is loaded and thus cause our main menu item to become visible.
                CommandID   menuID = new CommandID(GuidList.guidDynamicStartItemExamplePackageCmdSet, PkgCmdIDList.mnuidMyMenu);
                MenuCommand menu   = new MenuCommand(null, menuID);
                mcs.AddCommand(menu);

                //Add the DynamicItemMenuCommand that will be responsible for the expansion of our root item into N items
                //at runtime. Where N is the value the user has entered into our tool window.
                CommandID dynamicItemRootId = new CommandID(GuidList.guidDynamicStartItemExamplePackageCmdSet, PkgCmdIDList.cmdidMyDynamicStartItem);
                DynamicItemMenuCommand dynamicMenuCommand = new DynamicItemMenuCommand(dynamicItemRootId,
                                                                                       IsValidDynamicItem,
                                                                                       OnInvokedDynamicItem,
                                                                                       OnBeforeQueryStatusDynamicItem);

                mcs.AddCommand(dynamicMenuCommand);
            }
        }
Exemplo n.º 2
0
        private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args)
        {
            DynamicItemMenuCommand matchedCommand = (DynamicItemMenuCommand)sender;

            matchedCommand.Enabled = true;
            matchedCommand.Visible = true;

            //The root item in the expansion won't flow through IsValidDynamicItem as it will match against the actual DynamicItemMenuCommand based on the
            //'root' id given to that object on construction, only if that match fails will it try and call the dynamic id check, since it won't fail for
            //the root item we need to 'special case' it here as MatchedCommandId will be 0 in that case.
            bool isRootItem   = (matchedCommand.MatchedCommandId == 0);
            int  idForDisplay = (isRootItem ? PkgCmdIDList.cmdidMyDynamicStartItem : matchedCommand.MatchedCommandId);

            matchedCommand.Text = FormDisplayTextFromCommandId(idForDisplay);

            //Clear this out here as we are done with it for this item.
            matchedCommand.MatchedCommandId = 0;
        }
Exemplo n.º 3
0
        private void OnInvokedDynamicItem(object sender, EventArgs args)
        {
            DynamicItemMenuCommand invokedCommand = (DynamicItemMenuCommand)sender;

            MessageBox.Show(String.Format("You invoked item '{0}'", invokedCommand.Text));
        }