我想在Visual Studio中右键单击某个文件扩展名时添加自定义菜单项。

似乎有一些帮助开源项目来实现这一目标,但我想问一下是否有人曾经使用它们,它们有多容易 - 你能帮助我并提供一个起点吗?

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

有帮助吗?

解决方案

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

扩展Visual Studio上下文菜单

其他提示

是的,最简单的方法是创建自定义宏来处理您的任务(在VB中)。

添加宏

首先选择工具>宏>宏 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

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

自定义上下文菜单

要做的第二项任务是将自定义宏添加到上下文菜单中;去: 工具>自定义

从“工具栏”列表中的列表中勾选上下文菜单。选项卡(包含所有上下文菜单的新工具栏应显示在主窗口上)并切换到“命令”按钮。标签。现在,从上下文菜单工具栏中选择:“项目和解决方案上下文菜单”>项目,然后从“命令”中将宏拖到它上面。标签。在右键菜单下更改其名称/图标/按钮。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top