添加Visual Studio 2008上下文菜单项的最简单方法是什么? [英] What is the easiest way to add a Visual Studio 2008 Context Menu Item?

查看:105
本文介绍了添加Visual Studio 2008上下文菜单项的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

似乎有一些辅助开源项目可以完成此任务,但是我想问问是否有人使用过它们,它们使用起来有多容易-您能帮我一个忙吗? >

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

解决方案

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

添加宏

首先选择 Tools> Macros> Macros 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文件则显示弹出窗口.

自定义上下文菜单

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

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

现在您可以测试并使用它了.您新添加的宏应出现在项目"上下文菜单中.玩得开心!

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

解决方案

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!

这篇关于添加Visual Studio 2008上下文菜单项的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆