Delete a menu in MFC

In MFC, how to remove a menu item of type POPUP. RemoveMenu () either accepts an identifier or a position. Since there is no identifier in the POPUP menu, the option on the left is with the position.

But I do not understand 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 don’t understand how to find the CMenu object for the test, so that I can call RemoveMenu (), passing the position β€œ2” for the submenu_3_item_1.

Any suggestion for this would be greatly appreciated.

Thanks!

+7
source share
3 answers

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

After changing the loaded menu, it will be destroyed when the menu object used for loading goes out of scope. You must change the menu that is currently in use.

Your menu is a pop-up part of the main menu, the second in position. It contains 5 elements, and the second one has another popup. As far as I understand, you want to remove this item and the popup for this item. To make it work, you will need to ask the 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 everyone by pointing to the main frame.

+7
source

'Test' is the third menu item (by position) in the top-level menu. It was simply visualized horizontally, not vertically. Assuming you have a top-level menu descriptor, use the same code that you would use to get the submenu as you would like to get the Test menu.

0
source

Try the following. First you get the Test submenu (index 2), and then after you tell him to delete his 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); 
0
source

All Articles