Inserting submenus in an existing SDI menu | CodeGuru

Inserting submenus in an existing SDI menu

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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
	removed!
		enum { PROGRAM_MENU, LAYER_MENU, KEYMAP_MENU, SAMPLE_MENU };

	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)
		pMenu->RemoveMenu(INSERT_POSITION, MF_BYPOSITION);

	// 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;
		objectMenu.LoadMenu(IDR_OBJECT_MENUS);
		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
		VERIFY(pMenu->InsertMenu(INSERT_POSITION, MF_BYPOSITION | MF_POPUP,
			(UINT)pPopupMenu->GetSafeHmenu(), strMenuItem));

		// remove from the other menu!
		objectMenu.RemoveMenu(iMenuToAdd, MF_BYPOSITION);
	}

	DrawMenuBar();
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.