We help IT Professionals succeed at work.

Check out this week's podcast, "Dairy Farms to Databases: Community's Hand in Technology"Listen Now


Inserting a CMenu in to main menu bar

Moo-Juice asked
Last Modified: 2013-11-20
Hi All,

I am using MFC in Visual Studio 2008.  I have a CMDIFrameWndEx which has a CMFCMenuBar as the app-wizard provides.

I also have a menu resource, IDR_SCENE.

At runtime, I want to add the IDR_SCENE menu dynamically to the main menu bar.  Declaring the menu in my class:

CMenu m_sceneMenu;

Open in new window

I then try and insert it like this during creation:

::InsertMenu(pParentWnd->GetMenuBar().GetHMenu(), 0, MF_BYPOSITION | MF_POPUP, reinterpret_cast<UINT_PTR>((HMENU)(*m_sceneMenu.GetSubMenu(0))), NULL);

Open in new window

But, I never, ever see it.  No errors occur.  I have done some searching and this appears to be the way to do it.

Can anyone assist?

Thanks in advance
Watch Question

In the debugger, make sure that the HWND for pParentWnd represents the expected window. The window hierarchy for MDI applications is a little bizarre.

You can use Spy++ to check the HWND of the actual window.

Also, did you check the return values for both of these calls to see if one or both are failing?
This one is on us!
(Get your first solution completely free - no credit card required)


Hi there Zoppo,

I'll try this when I get home from work and let you know.  Thanks for the info!

No problem - I hope it works. The new GUI implementation from MFC FeaturePack is quite different than the 'old' MFC's one and in my opinion it's even poor documented.

Just let me know if you have further problems ...


Hi Zoppo,

Thanks for that, it almost works fine.  Only problem is that I get a blank sub menu, and *then* the menu as a popup from that.  Calling GetSubMenu(0) just gives me the blank one, so I need to look at that.

Also, when the app closes it gets saved and is there by default when the application starts again, so I need to add code to remove any custom menu buttons prior to shutdown.

Other than that, you're solution works nicely, thankyou :)

hm, strange - the problem with the popup-sub menu IMO should be solved using 'GetSubMenu(0)'.

I tested it in my test app and this worked for me:

> m_wndMenuBar.InsertButton (CMFCToolBarMenuButton (0, menu, -1, _T ("&DynamicMenu")));

For the other problem: I would suggest to reset the menu at application startup. To do this you can add these two lines at the end of 'CMainFrame::LoadState':

> m_wndMenuBar.RestoreOriginalstate();
> OnResetMenu( 0, 0 );

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.