EDN Admin
Well-known member
I need to add/remove menu items from the Main Menu of an MFC (9) MDI app during start-up. What I used to do in MFC (8) SDI was to grab a CMenu * using CMainFrame::GetMenu () (it been a while - I may have had to take it from the MainFrames parent instead.) Anyway, no matter what I do to get a pointer to a main menus CMenu object, every time I try to perform some type of manipulation, the program ASSERTS because ::IsMenu () fails when examining the m_hMenu member (of the CMenu object.) <br/><br/>I have tried to do this stuff in the apps InitInstance, the also the views OnInitialUpdate. To retreive a pointer to the main menus CMenu object I have tried:<br/><br/><span style="font-size:x-small CMenu * pMenu = GetMenu (); // the most direct approach, ouch<br/><br/><span style="font-size:x-small CMenu * pMenu = pMainFrame -> GetMenu (); // try to get it from the MainFrame<br/><br/>CMFCMenuBar * pMenuBar = (CMFCMenuBar *)(pMainFrame -> GetMenuBar ()); // try the MenuBar<br/><span style="font-size:x-small CMenu * pMenuBarMenu = pMenuBar -> GetMenu ();<br/><span style="font-size:x-small <br/>These all return different pointers, each with a different m_hMenu value. Now for an example of actually trying to use one of these CMenu object. A simple call the GetMenuItemCount member:<span style="font-size:x-small
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small int<span style="font-size:x-small count = pMenu -> GetMenuItemCount(); <span style="font-size:x-small <span style="font-size:x-small
This function calls ::IsMenu (m_hMenu) as to establish the condition for an ASSERT (which always happens.)
<br/>Since I cant get this to work, there is no hope for doing anything productive. So... what I want to know is:<br/><br/>How do I get a pointer to the CMenu object that represents the Main Menu of an MFC (9) MDI program, such that its m_hMenu member is VALID.<br/><br/>Also, I have posted this the VC++ General Forum because I cant find a forum that is at all related to MFC<br/>
<span style="font-size:x-small
<br/>Since I cant get this to work, there is no hope for doing anything productive. So... what I want to know is:<br/><br/>How do I get a pointer to the CMenu object that represents the Main Menu of an MFC (9) MDI program, such that its m_hMenu member is VALID.<br/><br/>Also, I have posted this the VC++ General Forum because I cant find a forum that is at all related to MFC
Thx Scott, but that didnt work either... I create a test program (and created an OnInitialUpdate () in the view class, and added your code (actually call CView::OnInitialUpdate () first). The returned value of pMenu was NULL, so the call to GetMenuItemCount obviously blows up.<br/><br/>I am not too familiar with these forums, but when I hit the reply button it didnt do what I expected and so Ive just edited my original post. Is that bad?
View the full article
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small int<span style="font-size:x-small count = pMenu -> GetMenuItemCount(); <span style="font-size:x-small <span style="font-size:x-small
This function calls ::IsMenu (m_hMenu) as to establish the condition for an ASSERT (which always happens.)
<br/>Since I cant get this to work, there is no hope for doing anything productive. So... what I want to know is:<br/><br/>How do I get a pointer to the CMenu object that represents the Main Menu of an MFC (9) MDI program, such that its m_hMenu member is VALID.<br/><br/>Also, I have posted this the VC++ General Forum because I cant find a forum that is at all related to MFC<br/>
<span style="font-size:x-small
<br/>Since I cant get this to work, there is no hope for doing anything productive. So... what I want to know is:<br/><br/>How do I get a pointer to the CMenu object that represents the Main Menu of an MFC (9) MDI program, such that its m_hMenu member is VALID.<br/><br/>Also, I have posted this the VC++ General Forum because I cant find a forum that is at all related to MFC
Thx Scott, but that didnt work either... I create a test program (and created an OnInitialUpdate () in the view class, and added your code (actually call CView::OnInitialUpdate () first). The returned value of pMenu was NULL, so the call to GetMenuItemCount obviously blows up.<br/><br/>I am not too familiar with these forums, but when I hit the reply button it didnt do what I expected and so Ive just edited my original post. Is that bad?
View the full article