添加Visual Studio 2008上下文菜单项的最简单方法是什么?

时间:2020-12-07 05:55:41

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

我想在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

我研究过的是:http://www.codeplex.com/ManagedMenuExtension

2 个解决方案

#1


5  

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:

这是一个教程,解释了如何使用宏添加上下文菜单而不是创建Visual Studio加载项。希望能帮助到你:

Extending the Visual Studio Context Menus

扩展Visual Studio上下文菜单

#2


10  

Yeah, the easiest way is to create custom macro to handle your task (in VB).

是的,最简单的方法是创建自定义宏来处理您的任务(在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:

首先选择工具>宏>宏IDE(Alt + F11)。要清除所有内容,请添加一个新模块,例如“ContextMenu”,并输入以下代码:

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.

当然,您必须自定义处理所选文件名的方式。现在,它只会显示每个文件的弹出窗口,如果它是txt文件则不同。

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!

现在您已准备好测试并使用它。您新添加的宏应出现在“项目”上下文菜单中。玩得开心!

#1


5  

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:

这是一个教程,解释了如何使用宏添加上下文菜单而不是创建Visual Studio加载项。希望能帮助到你:

Extending the Visual Studio Context Menus

扩展Visual Studio上下文菜单

#2


10  

Yeah, the easiest way is to create custom macro to handle your task (in VB).

是的,最简单的方法是创建自定义宏来处理您的任务(在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:

首先选择工具>宏>宏IDE(Alt + F11)。要清除所有内容,请添加一个新模块,例如“ContextMenu”,并输入以下代码:

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.

当然,您必须自定义处理所选文件名的方式。现在,它只会显示每个文件的弹出窗口,如果它是txt文件则不同。

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!

现在您已准备好测试并使用它。您新添加的宏应出现在“项目”上下文菜单中。玩得开心!