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

发布于 2024-07-07 14:50:18 字数 266 浏览 8 评论 0原文

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

似乎有一些辅助开源项目可以完成此任务,但我想问是否有人曾经使用过它们,它们有多容易 - 你能帮助我并提供一个起点吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榆西 2024-07-14 14:50:18

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

自定义上下文菜单

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

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

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文