Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing menu in MFC

Tags:

visual-c++

mfc

In MFC how to remove a menu-item of POPUP type. RemoveMenu() either take ID or position. Since, there is no ID for POPUP menu, option left is by using position.

But I am not getting how and where I can call RemoveMenu().

File  Edit  Test
            Test_submenu_1
            Test_submenu_2
            Test_submenu_3 > submenu_3_item_1
            Test_submenu_4
            Test_submenu_5

I want to remove Test_submenu_3? I am not getting how do find CMenu object for Test so that I can call RemoveMenu() by passing position "2" for submenu_3_item_1.

Any suggestion for doing this will be greatly appreciated.

Thanks!

like image 374
Vivek Kumar Avatar asked Sep 26 '12 11:09

Vivek Kumar


2 Answers

You cannot use LoadMenu, since this function does just that.

After modifying loaded menu it is killed when menu object used to load it goes out of scope. You have to modify menu that is currently used.

Your menu is a popup part of the main menu, second in position. It contains 5 items and second one is another popup. To my understanding, you want to remove this item and popup of this item. To make it work you will have to ask main window for the current menu:

CMenu* pMenu = GetMenu(); // get the main menu
CMenu* pPopupMenu = pMenu->GetSubMenu(2);//(Test menu with item....)
pPopupMenu->RemoveMenu(2, MF_BYPOSITION);

Of course, this code is from the main frame. If you want to use it elsewhere, you will have to access all using pointer to the main frame.

like image 98
JohnCz Avatar answered Nov 15 '22 11:11

JohnCz


Try the below. You get the Test sub-menu first (index 2), then once you have that you tell it to remove its Test_submenu_3 submenu by position (also 2).

CMenu topMenu;
topMenu.LoadMenu(IDR_YOUR_MENU);
CMenu& testSubMenu = *topMenu.GetSubMenu(2);
testSubMenu.RemoveMenu(2,MF_BYPOSITION);
like image 26
Nerdtron Avatar answered Nov 15 '22 13:11

Nerdtron