Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

c# - Add an Item to the visual studio folder right-click menu within AddIn

I want to add a menuitem to the right-click=>Add menu in visual studio 2012 solution explorer. When click the custom item I can add a project with my template. I developed a Visual Studio Add-In to do it but I get into some trouble. I am able to add a menuitem to the right-click menu but I can't get it to meet my requirement.

  1. The menuitem should be the submenu of "Add". Not a root item.

  2. I also need the menuitem to show only when I right-click on a folder named "Areas". I don't want it to show when I right-click on other folders.

Here is my OnConnection function code. How can I change it to meet my requirement.

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = ((AddIn)addInInst);
        if (connectMode == ext_ConnectMode.ext_cm_UISetup)
        {
            object[] contextGUIDS = new object[] { };
            Commands2 commands = (Commands2)_applicationObject.Commands;

            //Place the command on the tools menu.
            //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
            var bars=((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars);

            Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = bars["MenuBar"];

            //Find the Tools command bar on the MenuBar command bar:
            //CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
            //CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
            // get popUp command bars where commands will be registered.
            CommandBars cmdBars = (CommandBars)(_applicationObject.CommandBars);
            //CommandBar vsBarItem = cmdBars["Item"]; //the pop up for clicking a project Item
            CommandBar vsBarFolder = cmdBars["Web Project Folder"];
            CommandBar vsBarWebFolder = cmdBars["Web Folder"];

            //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
            //  just make sure you also update the QueryStatus/Exec method to include the new command names.
            try
            {
                //Add a command to the Commands collection:
                Command command = commands.AddNamedCommand2(_addInInstance, "ModuleAddin", "Add a Project", "Executes the command for ModuleAddin", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                //Add a control for the command to the tools menu:
                if (command != null)
                {
                    //command.AddControl(toolsPopup.CommandBar, 1);
                    command.AddControl(vsBarFolder);
                    //CommandBarButton button = (CommandBarButton)command.AddControl(vsBarFolder, 3);
                    //button.BeginGroup = true;
                }
            }
            catch (System.ArgumentException argEx)
            {
                System.Diagnostics.Debug.Write("Exception in HintPaths:" + argEx.ToString());
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need an add-in for this.

Link: http://nickmeldrum.com/blog/how-to-run-powershell-scripts-from-solution-explorer-in-visual-studio-2010

Copy-pasted blog post...

Step 1: Adding "run powershell script" as an external tool

  1. In Visual Studio go to the menu: Tools | External Tools
  2. Click the "Add" button
  3. Add the following form values:

    • Title: "Run Powershell script in output window"
    • Command: "C:windowssystem32windowspowershellv1.0powershell.exe"
    • Arguments: " -file "$(ItemPath)"
    • Initial Directory: "$(ItemDir)"
    • Tick "Use Output window"
    • (Close on exit will now be automatically on)
  4. Click the "Apply" button

  5. Click the "Add" button

  6. Add the following form values:

    • Title: "Run powershell script outside of VS"
    • Command: "C:windowssystem32windowspowershellv1.0powershell.exe"
    • Arguments: " -file "$(ItemPath)"
    • Initial Directory: "$(ItemDir)"
    • Don't tick "Use Output window"
    • Tick "Close on exit"
  7. Click the "Ok" button

They should look something like this: >

Step 2: Weird Step, trust me!

Check the index position it is in the external tools list. By default mine are at positions 6 and 7. (I think by default Create GUID is no. 1!)

Step 3: Hook it up to the context menus

  1. Go to the menu: Tools | Customize | Commands
  2. Click the "Context menu" radio option
  3. Scroll down to "Project and Solution Context Menus | Item" (nightmare long menu, type "Proj" to get roughly to the right place)
  4. Click the "Add Command" button
  5. Select the category: "Tools" and Command: "External Command 7" (or whatever your position is you got from the "Weird Step 2")
  6. Hit the "Ok" button
  7. Then to set up the 2nd command:
  8. Select Category: "Tools" and Command: "External Command 8" (or whatever your position is for the other one)
  9. Hit the "Ok" button again
  10. Move them around till you are happy with their order (I usually put them somewhere below "Open With...") >

Step 4: Add your keyboard shortcuts

  1. Go to the menu: Tools | Options
  2. Select the Environment | Keyboard section
  3. Find the Tools.ExternalCommandN item in the list (nightmare long list again, type "Tools" to get you roughly there again)
  4. Select your shortcut key for each command: I like CTRL SHIFT P and CTRL SHIFT ALT P respectively

ohai


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...