Add-In: Create a menu shown initially, containing tools

512
3
07-28-2011 01:55 AM
FossiG_
New Contributor III
Hi,

using an add-in I created a toolbar and menu for ArcMap.

A toolbar has the attribute showInitially="true". So the toolbar is shown at startup, the user doesn't need to add it manually. Is there sth. similar for menus to show at startup?

When adding items to the menu, the wizard only provides button elements but no tools. Is there any way, to get tools into a menu? I'm thinking of menu items which could be checked and unchecked (like in View/Status Bar).

Thx,
Fossi
0 Kudos
3 Replies
JeffreyHamblin
New Contributor III
Hi Fossi,

See this thread, Modify-(add)-to-MAIN-Toolbar-with-Addinx-files for some info on getting a menu to show initially.
0 Kudos
FossiG_
New Contributor III
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
0 Kudos
JeffreyHamblin
New Contributor III
Hi Fossi,

Thank you for posting back with the additional information about adding tools to a main menu.

Also, you're right about that line in the GetMainMenuBar() method not being necessary. I had quickly adapted the code from the sample at Applying Startup Customizations, and just left that there.

Good weekend to you too!
0 Kudos