问题
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
回答1:
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
回答2:
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!
来源:https://stackoverflow.com/questions/210323/what-is-the-easiest-way-to-add-a-visual-studio-2008-context-menu-item