Hi Jeff,thank you so much for sharing your code! Quite a bit above my ArcObjects programming level. But it works just great.And it is even the solution to the second question: How to add tools to a menu?I extended your code a little, so here's the solution to both questions. Menu shows initially AND contains tools!!
//Q: http://forums.arcgis.com/threads/33463-Modify-%28add%29-to-MAIN-Toolbar-with-Addinx-files
public class TestAddToMainMenu : ESRI.ArcGIS.Desktop.AddIns.Extension
{
public TestAddToMainMenu()
{
}
protected override void OnStartup()
{
WireEvents();
}
protected override void OnShutdown()
{
}
private void WireEvents()
{
ArcMap.Events.OpenDocument +=
new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_OpenDocumentEventHandler(Events_OpenDocument);
ArcMap.Events.NewDocument +=
new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_NewDocumentEventHandler(Events_NewDocument);
IApplicationStatusEvents_Event appStatusEvents =
ArcMap.Application.Document.Parent as IApplicationStatusEvents_Event;
appStatusEvents.Initialized += new IApplicationStatusEvents_InitializedEventHandler(Events_Initialized);
}
void Events_Initialized()
{
// Need this to load menu when no document loaded at startup
LoadCustomMainMenu();
}
void Events_OpenDocument()
{
LoadCustomMainMenu();
}
void Events_NewDocument()
{
LoadCustomMainMenu();
}
private void LoadCustomMainMenu()
{
ICommandBar mainMenuBar = GetMainMenuBar();
UID uid = new UIDClass();
// make sure we got the main menu bar
if (mainMenuBar == null)
return;
// check if our custom menu is already there
string menuID = "REPLACE WITH MENU ADDIN ID"; // ID of Add-In Root Menu
ICommandBar myMenu = mainMenuBar.Find(menuID, false) as ICommandBar;
if (myMenu == null)
{
// Add Menu, if needed
uid.Value = menuID;
Object indexObj = Type.Missing; // adds menu at far right
myMenu = mainMenuBar.Add(uid, ref indexObj) as ICommandBar;
}
// add further items (this can be buttons AND tools *evil grin*)
uid.Value = "REPLACE WITH BUTTON ADDIN ID";
myMenu.Add(uid);
uid.Value = "REPLACE WITH TOOL ADDIN ID";
myMenu.Add(uid);
//[...] just add any further item, you need
((ICommandItem)mainMenuBar).Refresh();
}
private ICommandBar GetMainMenuBar()
{
try
{
//Grab the root menu bar
UID uid = new UIDClass();
uid.Value = "{1E739F59-E45F-11D1-9496-080009EEBECB}"; // Main menubar
ICommandBars cmdBars = ArcMap.Application.Document.CommandBars;
//ICommandItem commandItem = cmdBars.Find(uid, false, false); //????
return cmdBars.Find(uid, false, false) as ICommandBar;
}
catch
{
return null;
}
}
Was the next to last row of GetMainMenuBar of particular importance? //ICommandItem commandItem = cmdBars.Find(uid, false, false); //????Well, thanx again and a nice weekend!Fossi