Click here to Skip to main content
12,758,235 members (33,899 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 download
6 bookmarked
Posted 4 Aug 2013

Adding Dynamic Sub menu to the Menu, Add Event Handler for Each Individual Sub menu

, 4 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to add sub menus dynamically, adding event for them and also retrieving the menu name on click event of individual sub menu


This post discusses how to add sub menu dynamically to the main menu so pop up menu goes on increasing as user does some action on the main menu like hide in my case.


In one of the applications, there was a requirement of adding dynamic pop up menu to the bar, when user would click on hide menu. As the user goes on clicking on hide bars, the individual bars name should keep on getting added to the sub menu. Please refer to the snapshot above for more details.

Using the Code

The below code is useful to add sub menu to the menu dynamically on right click on each selected bar:

 void CMyListDlg::OnRclickList(NMHDR* pNMHDR, LRESULT* pResult)
    // TODO: Add your control notification handler code here
   int nIndex = m_List.GetSelectionMark();
   CString pString = m_List.GetItemText(nIndex,1);
   CMenu menu, * pSubMenu; 
   int pos=0; menu.LoadMenu(IDR_MENU1); 

Once the sub menu has been added to the main menu, we need to show the bar once user has clicked on each sub menu in the list.

In my case, almost 20 sub menus have been added to the list. So we have to use ON_COMMAND_RANGE to assign ID to each sub menu added to the list. It will help us to add our show bar code on clicking of individual hidden bar.

--- OnClickAdd(UINT id)
   CString str;
   MSG pMsg = AfxGetThreadState()->m_lastSentMsg;
   pMsg.wParam &= 0xFFFF;
   menu.GetMenuString(LOWORD(pMsg.wParam) , str,true);
  menu.GetMenuString(id , str,true);

The "str" will give you the sub menu name as per your selected sub menu in the list.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ashish Ranjan Mishra
Software Developer (Senior)
India India
Working as a software profesional in Pvt firm
More than 7+ yrs of exp in the field of C++,VC++,MFC,COM,SQL server etc

You may also be interested in...

Comments and Discussions

Newsmissing attachments Pin
Maimonides4-Aug-13 21:23
professionalMaimonides4-Aug-13 21:23 
GeneralRe: missing attachments Pin
skyformat99@gmail.com5-Aug-13 1:04
memberskyformat99@gmail.com5-Aug-13 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 5 Aug 2013
Article Copyright 2013 by Ashish Ranjan Mishra
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid