Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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.

Background

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); 
   menu.GetSubMenu(0)->GetSubMenu(0)->InsertMenu(pos,MF_BYPOSITION,ID_COMMAND_START,pString)) 
}

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.

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

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

License

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

Share

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

Comments and Discussions

 
Newsmissing attachments PinprofessionalMaimonides4-Aug-13 21:23 
GeneralRe: missing attachments Pinmemberskyformat99@gmail.com5-Aug-13 1:04 

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

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

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