Inserting a CMenu in to main menu bar

Moo-Juice
Moo-Juice used Ask the Experts™
on
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:

m_sceneMenu.LoadMenu(IDR_SCENE);
::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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?
Hi Moo-Juice,

in the MFC-FeaturePack Samples there's a demo application 'DynamicMenu' which describes how to do this.

In short here's how to do it:

 Add a message handler for the 'AFX_WM_RESETMENU'
To do so add a function prototy like this in the CMainFrame class declaration in 'mainfrm.h':
> afx_msg LRESULT OnResetMenu(WPARAM,LPARAM);

In 'mainfrm.cpp' add an entry to the message map for this message handler like this (only add the bold, none-italic line):
> BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWndEx)>  ...>  ON_REGISTERED_MESSAGE(AFX_WM_RESETMENU, OnResetMenu)
> END_MESSAGE_MAP()

Implement the menu in the message handler in 'mainfrm.cpp' like the attached code.


After you did this the menu still may not appear until you reset the menu, i.e. with 'Customize ...->Menue->Reset' or via registry.

Further this code will only add the IDR_SCENE to the main frame menu, not to the child frame's menu, so you will only see it when no document/view is open - but to do this for MDI child window's menus should work too if you implement it similar in 'CChildFrame' ...


Hope that helps,

ZOPPO







LRESULT CMainFrame::OnResetMenu(WPARAM,LPARAM)
{
    CMenu menu;
    menu.LoadMenu( IDR_SCENE );

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

    m_wndMenuBar.AdjustLayout();
    m_wndMenuBar.AdjustSizeImmediate();

    return 0;
}

Open in new window

Author

Commented:
Hi there Zoppo,

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

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

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 ...

Author

Commented:
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 );

ZOPPO

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial