Inserting submenus in an existing SDI menu

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

In the SDI app I am working on, I needed to be able to insert a new submenu in the apps main menu based on the currently selected object. (My app uses the nested splitter code found elsewhere on CodeGuru to provide multiple views into a selected sound on an advanced MIDI soundcard). I found no information on how to do it, so after a bit of trial and error I came up with the technique shown in the following code. Please note that this code is NOT directly reusable; but it should give you an idea of what needs to be done. The submenus are all contained in a separate menubar, IDR_OBJECT_MENUS; everything else should be clear to the experienced MFC user :')
// Add a an additional popup menu to the main menu which is 
// specific to the currently selected object type. If pViewClass is NULL,
// no menu is added.
void CMainFrame::AddViewMenu(CRuntimeClass* pViewClass)
	const int INSERT_POSITION = 3;
	const int DEFAULT_MENU_COUNT = 6; // INCOMPLETE - change when test menu is
	CMenu* pMenu = GetMenu();
	ASSERT(pMenu->GetMenuItemCount() == DEFAULT_MENU_COUNT
		|| pMenu->GetMenuItemCount() == DEFAULT_MENU_COUNT + 1);
	// If there is already an extra menu present, remove it
	if (pMenu->GetMenuItemCount() == DEFAULT_MENU_COUNT + 1)
	// Now based on the specific view type, add the appropriate menu
	int iMenuToAdd = -1;
	if (pViewClass == RUNTIME_CLASS(CProgramView))
		iMenuToAdd = PROGRAM_MENU;
	else if (pViewClass == RUNTIME_CLASS(CLayerView))
		iMenuToAdd = LAYER_MENU;
	else if (pViewClass != NULL)
		ASSERT(FALSE); // INCOMPLETE - other types!
	if (iMenuToAdd != -1)
		// get the menu to add
		CMenu objectMenu;
		CMenu* pPopupMenu = objectMenu.GetSubMenu(iMenuToAdd);
		ASSERT(pPopupMenu != NULL);
		// get its string - *^&) Windows can't figure it out for itself
		CString strMenuItem;
		objectMenu.GetMenuString(iMenuToAdd, strMenuItem, MF_BYPOSITION);
		// Add it
			(UINT)pPopupMenu->GetSafeHmenu(), strMenuItem));
		// remove from the other menu!
		objectMenu.RemoveMenu(iMenuToAdd, MF_BYPOSITION);


  • Don't get messages when adding a submenu from a DLL to the main menu!

    Posted by Legacy on 01/04/2002 12:00am

    Originally posted by: Juergen Suetterlin

    Hy folks,
    I'm trying to modify my main menu out of a dynamically loaded (runtime) DLL.

    My application uses loadlibrary() to load my DLL and can open it. The I call an exported function inside the DLL which adds a submenu to the main menu (procedure found also at codeguru). But when I select the added submenu all sub entries are grayed and the debuggers says this: "Warning: no message line prompt for ID 0x8015." but in the resource the item was alligned to an ID and the ID was alligned to a funciton.

    Can anyone help me?


  • Works fine now thnaks to Julien Roger's fix !!!

    Posted by Legacy on 07/11/2001 12:00am

    Originally posted by: Alf

    In fact it works without it on Win 95 / 98 / NT systems,
    but crashes on Win 2000 / XP systems. With the "fix" it
    works now fine !

  • Disable a sub menu at runtime

    Posted by Legacy on 01/10/2001 12:00am

    Originally posted by: Usha

    I want to disable a sub menu at Runtime. Can u help me with that


  • problem with Win95

    Posted by Legacy on 08/12/1999 12:00am

    Originally posted by: latha

    This code works fine with NT but gives an assertion failure when i try to access the sub menu in Win95. Please let me know a solution for the same.

  • ON_UPDATE_COMMAND_UI does not work on the inserted menu

    Posted by Legacy on 03/24/1999 12:00am

    Originally posted by: David Geldreich

    If you try to associate an ON_UPDATE_COMMAND_UI on one of the command included in the inserted menu command routing will work but in ON_UPDATE_COMMAND_UI, pCmdUI->Enable(FALSE) will not work.

    It is because things like pCmdUI->m_pMenu->EnableMenuItem(pCmdUI->m_nID, MF_GRAYED|MF_DISABLED|MF_BYCOMMAND); will return -1.

    One solution is to keep a pointer to the inserted menu and do ourself, change pCmdUI->m_pMenu before calling CCmdUI functions.

  • This doesn't work

    Posted by Legacy on 12/25/1998 12:00am

    Originally posted by: Vasu Y

    This piece of code doesn't work for me.
    It is adding the new Pop-up menu to the Main menu, but if you click on the newly added pop-up menu, it is giving a Debug-Assertion Failure.

    Can somebody help me out.
    The following is the piece of code.

    CMenu* pMainMenu = pMainFrame->GetMenu();
    short iMenuToAdd;
    iMenuToAdd = 0;
    CMenu objectMenu;
    CMenu* pPopupMenu = objectMenu.GetSubMenu(iMenuToAdd);
    ASSERT(pPopupMenu != NULL);
    short count = 0;
    count = pPopupMenu->GetMenuItemCount();
    // get its string - *^&) Windows can't figure it out for itself
    CString strMenuItem;
    objectMenu.GetMenuString(iMenuToAdd, strMenuItem, MF_BYPOSITION);

    VERIFY(pMainMenu->AppendMenu( MF_POPUP,
    (UINT)pPopupMenu->Detach(), strMenuItem));

  • modifying SDI/MDI menus

    Posted by Legacy on 10/13/1998 12:00am

    Originally posted by: Brian Smith

    Instead of

    (UINT)pPopupMenu->GetSafeHmenu(), strMenuItem)); // remove from the other menu!
    objectMenu.RemoveMenu(iMenuToAdd, MF_BYPOSITION);

    you could call:

    (UINT)pPopupMenu->Detach(), strMenuItem));

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The software-defined data center (SDDC) and new trends in cloud and virtualization bring increased agility, automation, and intelligent services and management to all areas of the data center. Businesses can now more easily manage the entire lifecycle of their applications and services via the SDDC. This Aberdeen analyst report examines how a strong foundation in both the cloud and internal data centers is empowering organizations to fully leverage their IT infrastructure and is also preparing them to be able …

  • Microsoft Azure® is a leading choice for businesses looking to take advantage of the cloud. Azure is particularly appealing to businesses that have already invested in Microsoft on-premises and are now considering running these applications and other workloads in the cloud. To understand how to make this move to Azure, many businesses are turning to managed service providers (MSPs) with specific Azure expertise. Read this white paper to learn the eight key areas to focus on when considering an MSP for an …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date