Adding a submenu to the Tools menu for (C#) add-ins
-
2008年3月28日 13:32
The code generated by the wizard for (C#) Add-ins places a command on the Tools menu in Visual Studio for the add-in.
I would like instead, to have a submenu, exposing the functionality of my add-in and not just one command.
Tools
My Menu
My Submenu item1
My Submenu item2
My Submenu item3
etc.
I've looked through the MSDN but can't seem to find a references on how to achieve this!
回答
-
2008年3月28日 15:53モデレータ
Hi Taime,
I recently built a new add-in that does this by creating a CommandBarPopup control. The code below creates CommandBarButton objects and doesn't use named commands, as I like to only have the menus shows when I actually have the addin loaded (keeps the environment a little less cluttered).
Code Snippetpublic class Connect : IDTExtensibility2
{
private DTE2 _applicationObject;
private AddIn _addInInstance;
private CommandBarPopup _cmdBarPopup;
private CommandBarButton _btnFreebie;
private CommandBarButton _btnLicensed;
public Connect() {}
public void OnConnection(object application, ext_ConnectMode connectMode,
object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Add temporary commandbar and buttons
try
{
CommandBar menuBar = ((CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarPopup toolsPopup = (CommandBarPopup)menuBar.Controls["Tools"];
// Add a popup control to group our buttons under
_cmdBarPopup = (CommandBarPopup)toolsPopup.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
_cmdBarPopup.Caption = "Licensed Add-In";
_btnFreebie = (CommandBarButton)_cmdBarPopup.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
_btnFreebie.Style = MsoButtonStyle.msoButtonIconAndCaption;
_btnFreebie.Caption = "Freebie";
_btnFreebie.TooltipText = "This command doesn't require a license";
_btnFreebie.Picture = ImageConverter.ImageToIPicture(Resources.Resource.BarCode);
_btnFreebie.Click += new _CommandBarButtonEvents_ClickEventHandler(_btnFreebie_Click);
_btnLicensed = (CommandBarButton)_cmdBarPopup.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
_btnLicensed.Caption = "Licensed";
_btnLicensed.TooltipText = "This command requires a license";
_btnLicensed.FaceId = 1845;
_btnLicensed.Click += new _CommandBarButtonEvents_ClickEventHandler(_btnLicensed_Click);
}
catch
{
}
}
.........
The ImageConverter class lets me put a custom icon image into the button, and looks like the following. Note, you'll need to add a reference to stdole.dll.
Code Snippetusing
System;using
System.Collections.Generic;using
System.Text;using
System.Windows.Forms;using
System.Drawing;namespace
LicensedAddIn{
class ImageConverter : AxHost{
public ImageConverter() : base("59EE46BA-677D-4d20-BF10-8D8067CB8B33"){
}
public static stdole.StdPicture ImageToIPicture(Image image){
return (stdole.StdPicture)AxHost.GetIPictureDispFromPicture(image);}
}
}
Hopefully that'll get you started,
すべての返信
-
2008年3月28日 15:23
I'm not sure about how to do this from the wizard generated code because I never use the wizard but here is some code that will add submenus to a menu in the main tool menu. You should be able to get it going from there. The code created a "New Addin" menu under the Tool menu. Then adds an "About" menu under the "New Addin" menu.
CommandBar menuBarCommandBar = ((CommandBars) _applicationObject.CommandBars)["MenuBar"]; //Find the Tools command bar on the MenuBar command bar: CommandBarControl toolsControl = menuBarCommandBar.Controls["Tools"]; CommandBarPopup toolsPopup = (CommandBarPopup) toolsControl;
try { try {addInCommandBar = ((
CommandBars) _applicationObject.CommandBars)["New Addin"];}
catch { //throws and exception the first time the addin is run because the commandbar doesn't exist}
if (addInCommandBar == null) {addInCommandBar =
(
CommandBar)commands.AddCommandBar(
"New Addin", vsCommandBarType.vsCommandBarTypeMenu,toolsPopup.CommandBar, 1);
}
}
catch {}
try {
try {aboutCommand = ((
Commands2) _applicationObject.DTE.Commands).Item( "AddInAboutButton", -1);}
catch {}
if (aboutCommand == null) { //Add a command to the Commands collection:aboutCommand =
commands.AddNamedCommand2(_addInInstance,
"AddInAboutButton", "About", "About Box for the AddIn", false, 0, ref contextGUIDS,(
int) vsCommandStatus.vsCommandStatusSupported +(
int) vsCommandStatus.vsCommandStatusEnabled,(
int) vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton); //Add a control for the command to the tools menu: if ((aboutCommand != null) && (refAssistCommandBar != null)) {aboutCommand.AddControl(addInCommandBar, 1);
}
}
}
catch {}
-
2008年3月28日 15:53モデレータ
Hi Taime,
I recently built a new add-in that does this by creating a CommandBarPopup control. The code below creates CommandBarButton objects and doesn't use named commands, as I like to only have the menus shows when I actually have the addin loaded (keeps the environment a little less cluttered).
Code Snippetpublic class Connect : IDTExtensibility2
{
private DTE2 _applicationObject;
private AddIn _addInInstance;
private CommandBarPopup _cmdBarPopup;
private CommandBarButton _btnFreebie;
private CommandBarButton _btnLicensed;
public Connect() {}
public void OnConnection(object application, ext_ConnectMode connectMode,
object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Add temporary commandbar and buttons
try
{
CommandBar menuBar = ((CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarPopup toolsPopup = (CommandBarPopup)menuBar.Controls["Tools"];
// Add a popup control to group our buttons under
_cmdBarPopup = (CommandBarPopup)toolsPopup.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
_cmdBarPopup.Caption = "Licensed Add-In";
_btnFreebie = (CommandBarButton)_cmdBarPopup.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
_btnFreebie.Style = MsoButtonStyle.msoButtonIconAndCaption;
_btnFreebie.Caption = "Freebie";
_btnFreebie.TooltipText = "This command doesn't require a license";
_btnFreebie.Picture = ImageConverter.ImageToIPicture(Resources.Resource.BarCode);
_btnFreebie.Click += new _CommandBarButtonEvents_ClickEventHandler(_btnFreebie_Click);
_btnLicensed = (CommandBarButton)_cmdBarPopup.Controls.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
_btnLicensed.Caption = "Licensed";
_btnLicensed.TooltipText = "This command requires a license";
_btnLicensed.FaceId = 1845;
_btnLicensed.Click += new _CommandBarButtonEvents_ClickEventHandler(_btnLicensed_Click);
}
catch
{
}
}
.........
The ImageConverter class lets me put a custom icon image into the button, and looks like the following. Note, you'll need to add a reference to stdole.dll.
Code Snippetusing
System;using
System.Collections.Generic;using
System.Text;using
System.Windows.Forms;using
System.Drawing;namespace
LicensedAddIn{
class ImageConverter : AxHost{
public ImageConverter() : base("59EE46BA-677D-4d20-BF10-8D8067CB8B33"){
}
public static stdole.StdPicture ImageToIPicture(Image image){
return (stdole.StdPicture)AxHost.GetIPictureDispFromPicture(image);}
}
}
Hopefully that'll get you started,
-
2008年3月28日 16:38Thanks Ed! Thats exactly what I was looking for!

