Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 400
Adam Avatar asked Dec 31 '22 09:12

Adam


1 Answers

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!

like image 196
juckobee Avatar answered Jan 14 '23 14:01

juckobee