Visual Studio 2010 Plug-in - Adding a Context Menu to Solution Explorer

I want to add a new option to the context menu of the Visual Studio 2010 browser for a specific file type. For example, right-clicking on the * .cs file will show the existing context menu plus "my new option".

I am wondering what the code will look like; and would love a pointer to a good link for developing visual studio plugins. The tutorials / links that I see are clearly terrible.

Thank!

+45
c # plugins visual-studio visual-studio-2010
Jun 10 '10 at 17:55
source share
4 answers

I found that the best way is to make a Visual Studio package instead of a Visual Studio add-in. The vsix deployment experience is so smooth - it was all very simple. It only supports Visual Studio 2010, but it was good enough in my case.

Here is the vsct result:

<Commands package="guidBingfooPluginPkg"> <Groups> <Group guid="guidBingfooPluginCmdSet" id="MyMenuGroup" priority="0x0600"> <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/> </Group> </Groups> <Buttons> <Button guid="guidBingfooPluginCmdSet" id="cmdidfooLocalBox" priority="0x0100" type="Button"> <Parent guid="guidBingfooPluginCmdSet" id="MyMenuGroup" /> <!-- <Icon guid="guidImages" id="bmpPic1" /> --> <CommandFlag>DynamicVisibility</CommandFlag> <Strings> <CommandName>cmdidfooLocalBox</CommandName> <ButtonText>View in foo</ButtonText> </Strings> </Button> <Button guid="guidBingfooPluginCmdSet" id="cmdidfooTestBed" priority="0x0100" type="Button"> <Parent guid="guidBingfooPluginCmdSet" id="MyMenuGroup" /> <CommandFlag>DynamicVisibility</CommandFlag> <Strings> <CommandName>cmdidfooTestBed</CommandName> <ButtonText>View in foo on Test Beds</ButtonText> </Strings> </Button> </Buttons> <Bitmaps> <Bitmap guid="guidImages" href="Resources\Images_32bit.bmp" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows"/> </Bitmaps> </Commands> <Symbols> <GuidSymbol name="guidBingfooPluginPkg" value="{62c4a13c-cc61-44a0-9e47-33111bd323ce}" /> <GuidSymbol name="guidBingfooPluginCmdSet" value="{59166210-d88c-4259-9809-418bc332b0ab}"> <IDSymbol name="MyMenuGroup" value="0x1020" /> <IDSymbol name="cmdidfooLocalBox" value="0x0100" /> <IDSymbol name="cmdidfooTestBed" value="0x0101" /> </GuidSymbol> <GuidSymbol name="guidImages" value="{2dff8307-a49a-4951-a236-82e047385960}" > <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" /> </GuidSymbol> </Symbols> </CommandTable> 
+16
Aug 09 2018-10-10T00:
source share

It took me about 5 hours to do this.

There are two options: the Visual Studio add-in (or general add-in) and the Visual Studio package.

The package is much more complicated to give you much more control, but it is not needed for the context menu in the solution explorer.

So, a new project-> Other types of projects β†’ Extensibility β†’ Visual Studio Add-in.

Here's the walkthrough - Link

Also this one I followed some - Link

I recommend that you leave the option to add to the tool menu until the context menu works, or to provide a place to set the settings dialog box (if you do not click the Tool-> Tool page.

Here's the connection code:

  _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; if (connectMode == ext_ConnectMode.ext_cm_UISetup) { object[] contextGUIDS = new object[] { }; Commands2 commands = (Commands2)_applicationObject.Commands; string toolsMenuName = "Tools"; //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: Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["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 vsBarWebItem = cmdBars["Web Item"]; CommandBar vsBarMultiItem = cmdBars["Cross Project Multi Item"]; CommandBar vsBarFolder = cmdBars["Folder"]; CommandBar vsBarWebFolder = cmdBars["Web Folder"]; CommandBar vsBarProject = cmdBars["Project"]; //the popUpMenu for right clicking a project CommandBar vsBarProjectNode = cmdBars["Project Node"]; //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, "HintPaths", "HintPaths", "Executes the command for HintPaths", 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) && (toolsPopup != null)) { //command.AddControl(toolsPopup.CommandBar, 1); command.AddControl(vsBarProject); } } catch (System.ArgumentException argEx) { System.Diagnostics.Debug.Write("Exception in HintPaths:" + argEx.ToString()); //If we are here, then the exception is probably because a command with that name // already exists. If so there is no need to recreate the command and we can // safely ignore the exception. } } } 

This code checks to see if what the user has selected, for example, the project:

  private Project GetProject() { if (_applicationObject.Solution == null || _applicationObject.Solution.Projects == null || _applicationObject.Solution.Projects.Count < 1) return null; if (_applicationObject.SelectedItems.Count == 1 && _applicationObject.SelectedItems.Item(1).Project != null) return _applicationObject.SelectedItems.Item(1).Project; return null; } 

Please note that some string names in your code should match, and I'm not sure which ones they haven't done yet, since I just did it yesterday.

+32
Jun 14 '10 at 18:34
source share

UPDATE:

GAX / GAT for VS2010 is also available from http://msdn.microsoft.com/en-us/library/ff687173

ORIGINAL MAIL

Well, that’s terrible because VS is really complicated. Using GAX / GAT is possible, but there is no VS2010 version . I suggest downloading some samples from the Visual Studio Gallery to try to understand how all this works, unfortunately, is not an easy task.

NTN

+3
Jun 10 2018-10-10T00:
source share

I had to add an item to the context menu of the code editor window, which turned out to be cmdBars["Script Context"] , because I wanted it specifically for JavaScript files.

As a search method for this that I felt was useful sharing, I added a new menu item to all the controls (456) in visual studio with the following loop:

 foreach (CommandBar cc in cmdBars) { if (cc.Index >= 1 && cc.Index <= 456) { command.AddControl(cmdBars[cc.NameLocal]); } } 

Then I narrowed it down using the division and subjugation technique, adjusting the boundaries of the loop:

  if (cc.Index >= 1 && cc.Index <= 256) ... if (cc.Index >= 1 && cc.Index <= 128) ... if (cc.Index >= 64 && cc.Index <= 128) ...etc... 

Until I found what I was looking for.

(A related question is in the Visual Studio 2010 Plug-in - adding a context menu to the editor window )

+2
Jul 29 2018-12-12T00:
source share



All Articles