在 Visual Studio .NET IDE 中向上下文菜单添加菜单项






4.09/5 (5投票s)
本文档描述了如何在 VS .NET IDE 的代码窗口的上下文菜单中添加自定义菜单项和子菜单项。
引言
通常情况下,将我们的自定义功能集成到开发环境中是期望的。Add-in 是一种强大的实现方式。 我曾经在项目中有一个需求,需要在 IDE 的代码编辑器中的上下文菜单中添加一个菜单项。 因此,在本节中,我将讨论如何在 IDE 的上下文菜单中添加菜单项。
背景
我假设您具备创建 Visual Studio Add-in 的足够专业知识。
使用代码
在 IDE 中打开一个 Visual Studio Add-in 项目。 这将带您进入一个向导。 按照向导提供的默认设置进行操作。 您将有一个选项来输入要创建的 Add-in 的名称和描述。 我将 Add-in 的名称设置为 CodeProjectAddin,并描述了我们的需求。 这是将在 Add-in 管理器中显示的名称。 使用适当的值填写名称和描述字段。 在下一个屏幕上,您将可以选择 Add-in 选项。 请参阅下图配置此向导屏幕。 实际上,我们不会将此 Add-in 添加到我们的工具菜单项。 此示例将向代码窗口的上下文菜单添加一个菜单项。 因此,请选中在启动时加载 Add-in 的选项并继续。
完成向导后,将创建一个包含 Connect.cs 中类的项目。
在 Connect.cs 中有一个名为 OnConnection()
的事件处理程序。 这是我们在启动时加载 Add-in 时触发的事件。 因此,我们必须在这里编写代码来实现我们的功能。
我的 OnConnection
事件处理程序如下所示
// Get the CommandBar for the window in which
// you want to add the menuitem. I am going to add it to the code window.
CommandBar oCommandBar = applicationObject.CommandBars["Code Window"];
// Note: The expression Code Window has to be used exactly.
// This is the .Net will identify
// the window to which we are going to add the Add=in.
// I will list down all the collection
// of CommandBars in my application later.
// I am going to add a MenuItem and a submenuitem to that.
// So I go ahead and create a PopUp Item.
CommandBarPopup oPopup = (CommandBarPopup)
oCommandBar.Controls.Add(MsoControlType.msoControlPopup,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,1,true);
// Set the caption of the menuitem
oPopup.Caption = "Get Command Bars";
// Now I go ahead and add a Submenu item to the added Menuitem.
CommandBarControl oControl =
oPopup.Controls.Add(MsoControlType.msoControlButton,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,1,true);
// Set the caption of the submenuitem
oControl.Caption = "Get Command Bars";
// Now that we have added the menu items,
// we will associate the click events for these items.
// I will associate a click event only for the SubMenuitem at present.
oSubMenuItemHandler =
applicationObject.Events.get_CommandBarEvents(oControl);
oSubMenuItemHandler.Click += new
_dispCommandBarControlEvents_ClickEventHandler(oSubMenuItemHandler_Click);
// we can create a click event for the menuitem in the same way.
子菜单项单击事件的事件处理程序如下所示。 为了处理单击事件,我添加了一个 Windows Forms 控件,并使用 Commandbars
集合填充了列表框。
//The event handler associated with the sub menu item.
CommandBarEvents oSubMenuItemHandler;
// event handler for Click event.
protected void oSubMenuItemHandler_Click(object CommandaBarControl,
ref bool handled,ref bool cancelDefault)
{
System.Collections.ArrayList oList =
new System.Collections.ArrayList();
// I am getting all the command bar items
// in the command bar collection.
foreach(CommandBar oCommandBar in applicationObject.CommandBars)
{
oList.Add(oCommandBar.Name);
}
frmCommandBarList ofrmCommandBarList = new frmCommandBarList();
ofrmCommandBarList.SetListBoxList = oList;
ofrmCommandBarList.ShowDialog();
// We have to use the exact term as listed
// in the listbox, if we want to add
// the context menu to some other window.
}
编译源代码并安装设置。 要选择此 Add-in,请转到“工具”->“Add-in 管理器”。
将显示我们创建的 Add-in。 单击它旁边的复选框。 这将启用您的 Add-in。 现在右键单击代码编辑器窗口。 将添加一个新的菜单项。 单击子菜单项。 将启动一个 Windows 应用程序,并列出 CommandBars
集合中所有可用的命令栏。
关注点
当我们要将菜单项添加到任何其他窗口时,例如数据库项目中的 SQL 查询窗格窗口,我们必须从命令栏集合中获取确切的项,即“Query SQL Pane”。
编码愉快!