VS Add Ins and nested popu up menus
Few days ago I was trying to find the way to add a nested popup menu in Visual Studio’s Tools menu using VS Add-in.
Google referred me to this useful answer to the question Is it possible to add nested menu items?. The answer was meant for VS 2003.
VS 2005 apparently doesn’t use Office CommandBar anymore. It has its own. So the code that creates and inserts a popup menu in one of the main menus in VS 2005, remains mostly unchanged, uses new namespace (Microsoft.VisualStudio.CommandBars instead of Microsoft.Office.Core) and requires some new casts. For completeness sake here is the whole function:
using Microsoft.VisualStudio.CommandBars;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
private DTE2 applicationObject = (DTE2)application;;
private AddIn addInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] {};
try
{
object nullPar = Type.Missing;
//Create a menu item under Tools menu
CommandBar cmdbar = (CommandBar) ((CommandBars)applicationObject.CommandBars) ["Tools"];
CommandBarPopup popup = (CommandBarPopup)cmdbar.Controls.Add (MsoControlType.msoControlPopup, nullPar, nullPar, 2, nullPar);
popup.Caption = "My Popup";
//Create commands and then add them to the menu created above
Commands2 commands = (Commands2)applicationObject.Commands;
Command cmd = commands.AddNamedCommand2(addInInstance, "CommandName","Text","ToolTip",true,59,ref contextGUIDS,
(int) vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
cmd.AddControl(popup.CommandBar, 1);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
}
Google referred me to this useful answer to the question Is it possible to add nested menu items?. The answer was meant for VS 2003.
VS 2005 apparently doesn’t use Office CommandBar anymore. It has its own. So the code that creates and inserts a popup menu in one of the main menus in VS 2005, remains mostly unchanged, uses new namespace (Microsoft.VisualStudio.CommandBars instead of Microsoft.Office.Core) and requires some new casts. For completeness sake here is the whole function:
using Microsoft.VisualStudio.CommandBars;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
private DTE2 applicationObject = (DTE2)application;;
private AddIn addInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] {};
try
{
object nullPar = Type.Missing;
//Create a menu item under Tools menu
CommandBar cmdbar = (CommandBar) ((CommandBars)applicationObject.CommandBars) ["Tools"];
CommandBarPopup popup = (CommandBarPopup)cmdbar.Controls.Add (MsoControlType.msoControlPopup, nullPar, nullPar, 2, nullPar);
popup.Caption = "My Popup";
//Create commands and then add them to the menu created above
Commands2 commands = (Commands2)applicationObject.Commands;
Command cmd = commands.AddNamedCommand2(addInInstance, "CommandName","Text","ToolTip",true,59,ref contextGUIDS,
(int) vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
cmd.AddControl(popup.CommandBar, 1);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
}
2 Comments:
Thanks, works great.
Very easy and nice article
my respect!
Post a Comment
<< Home