【发布时间】:2010-12-07 06:19:44
【问题描述】:
我正在尝试为 Visual Studio 2008 插件创建基本菜单结构。到目前为止,我可以创建第一个顶级菜单,在下面的示例中我称之为 TOPMENU。我正在努力弄清楚如何将子项目添加到 TOPMENU。我已经尝试了多种方法,但无法弄清楚。有人可以帮我完成下面的代码吗?当我尝试在下面创建对象“a2”时,我目前遇到了无效的强制转换异常。
void IDTExtensibility2.OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_ApplicationObject = (DTE2)application;
_AddInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_ApplicationObject.Commands;
CommandBar cbMainMenu = ((CommandBars)_ApplicationObject.CommandBars)["MenuBar"];
try
{
// ROOT MENU
Command cmdProjectManagement = commands.AddNamedCommand2(_AddInInstance, "TOPMENU", "TOPMENU", "",
true, null, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
if (cmdProjectManagement != null)
cmdProjectManagement.AddControl(cbMainMenu, cbMainMenu.Controls.Count);
// SUB ITEM
Command cmdCompiledAssemblies = commands.AddNamedCommand2(_AddInInstance, "TOPMENU_CompiledAssemblies", "Compiled Assemblies", String.Empty,
true, null, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
CommandBarControl a1 = cbMainMenu.Controls["TOPMENU"];
CommandBarPopup a2 = (CommandBarPopup)a1;
if (cmdCompiledAssemblies != null)
cmdCompiledAssemblies.AddControl(a2.CommandBar, 1);
}
catch (Exception ex)
{
}
}
}
【问题讨论】:
-
您是如何在 TopMenu 下方创建多个子菜单和分隔线的?
标签: visual-studio-2008 menu add-in