private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args) { AttachSubmenuCommand matchedCommand = (AttachSubmenuCommand)sender; matchedCommand.Enabled = true; matchedCommand.Visible = true; // Find out whether the command ID is 0, which is the ID of the root item. // If it is the root item, it matches the constructed DynamicItemMenuCommand, // and IsValidDynamicItem won't be called. bool isRootItem = (matchedCommand.MatchedCommandId == 0); // The index is set to 1 rather than 0 because the Solution.Projects collection is 1-based. int indexForDisplay = (isRootItem ? 1 : (matchedCommand.MatchedCommandId - (int)cmdidMyCommand) + 1); matchedCommand.Text = dte2.Solution.Projects.Item(indexForDisplay).Name; Array startupProjects = (Array)dte2.Solution.SolutionBuild.StartupProjects; string startupProject = System.IO.Path.GetFileNameWithoutExtension((string)startupProjects.GetValue(0)); // Check the command if it isn't checked already selected matchedCommand.Checked = (matchedCommand.Text == startupProject); // Clear the ID because we are done with this item. matchedCommand.MatchedCommandId = 0; }
/// <summary> /// Initializes a new instance of the <see cref="AttachToEngineCommand"/> class. /// Adds our command handlers for menu (commands must exist in the command table file) /// </summary> /// <param name="package">Owner package, not null.</param> /// <param name="commandService">Command service to add command to, not null.</param> private AttachToEngineCommand(AsyncPackage package, OleMenuCommandService commandService) { this.package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); if (package == null) { throw new ArgumentNullException(nameof(package)); } this.package = package; dte2 = (DTE2)(package as System.IServiceProvider).GetService(typeof(DTE)); if (commandService != null) { // Add the DynamicItemMenuCommand for the expansion of the root item into N items at run time. CommandID dynamicItemRootId = new CommandID(new Guid(guidDynamicMenuPackageCmdSet), (int)cmdidMyCommand); AttachSubmenuCommand dynamicMenuCommand = new AttachSubmenuCommand(dynamicItemRootId, IsValidDynamicItem, OnInvokedDynamicItem, OnBeforeQueryStatusDynamicItem); commandService.AddCommand(dynamicMenuCommand); AttachCommand = dynamicMenuCommand; // add,find不到 CommandID defaultCommandId = new CommandID(new Guid(guidDynamicMenuPackageCmdSet), (int)cmdidMyAnchorCommand); MenuCommand defaultMenuCommand = new MenuCommand(this.OnDefaultCommandExecute, defaultCommandId); commandService.AddCommand(defaultMenuCommand); //CommandID startCommand = new CommandID(new Guid("{5EFC7975-14BC-11CF-9B2B-00AA00573819}"), (int)VSStd97CmdID.Start); //foreach(Command command in dte2.Commands) //{ // if(command.ID == (int)VSStd97CmdID.Start) // { // (command as MenuCommand) // int a = 1; // } //} //MenuCommand startMenu = commandService.FindCommand(startCommand); //if(startMenu != null) //{ // startMenu.Visible = false; //} } //var menuCommandID = new CommandID(CommandSet, CommandId); //var menuItem = new MenuCommand(this.Execute, menuCommandID); //commandService.AddCommand(menuItem); }
private void OnInvokedDynamicItem(object sender, EventArgs args) { AttachSubmenuCommand invokedCommand = (AttachSubmenuCommand)sender; // If the command is already checked, we don't need to do anything if (invokedCommand.Checked) { return; } // Find the project that corresponds to the command text and set it as the startup project var projects = dte2.Solution.Projects; foreach (Project proj in projects) { if (invokedCommand.Text.Equals(proj.Name)) { dte2.Solution.SolutionBuild.StartupProjects = proj.FullName; return; } } }