Tuesday, May 3, 2011

What is the easiest way to add a Visual Studio 2008 Context Menu Item?

I would like to add a custom menu item when you right-click a certain file extension in Visual Studio.

There seem to be some helper open source projects to accomplish this, but I'd like to ask if anyone has ever used them, and how easy were they - and can you help me and provide a starting point?

One I've researched is: http://www.codeplex.com/ManagedMenuExtension

From stackoverflow
  • Here's a tutorial that explains how to add a Context Menu Using a Macro instead of creating a Visual Studio Add-in. Hope it helps:

    Extending the Visual Studio Context Menus

    Adam : Works like a charm. Thanks.
    Rohit : The link isn't working for me. Any alternatives?
  • Yeah, the easiest way is to create custom macro to handle your task (in VB).

    Adding macro

    First of all select Tools>Macros>Macros IDE (Alt+F11). To make everything clear, add a new module for example "ContextMenu" and put in it the following code:

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports System.Diagnostics
    
    Public Module ContextMenu
    
    Public Sub DoSomething()
        'Few declarations'
        Dim SolutionExplorer As UIHierarchy
        Dim Item As UIHierarchyItem
        Dim SelectedItem As EnvDTE.ProjectItem
    
        'Getting the solution explorer'
        SolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
    
        'Iterating through all selected items'
        For Each Item In SolutionExplorer.SelectedItems
            'Getting the item'
            SelectedItem = CType(Item.Object, EnvDTE.ProjectItem)
    
            'Do some stuff here'
            If SelectedItem.FileNames(1).EndsWith("txt") Then
                MsgBox("We got the text file!", , SelectedItem.FileNames(1))
            Else
                MsgBox("We got something else...", , SelectedItem.FileNames(1))
            End If
        Next
    End Sub
    End Module
    

    Of course, you have to customize the way you're handling selected filenames. For now, it will just show a popup for every file, different if it will be txt file.

    Customizing the context menu

    The second task to do is to add your custom macro to the context menu; go to: Tools>Customize

    Tick the context menus from the list on "Toolbars" tab (the new toolbar with all context menus should appear on main window) and switch to "Commands" tab. Now, from context menus toolbar select: "Project and Solution Context Menus">Item and drag your macro onto it from "Commands" tab. Change its name/icon/button under right click menu.

    Now you are ready to test and use it. Your newly added macro should appear in Item context menu. Have a fun!

    Adam : I'm not seeing my custom macro as an available option when I select the 'Macros' category in the Commands tab. I see all the sample macros but not my custom one. Is there any extra steps to saving/building/adding a macro?
    juckobee : The macro won't appear in command menu if it gets any parameter. Try to declare: Public Sub theMacroName()
  • How can it be done say part of the Add-in installation?

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.