How to create a menu and submenu on right click in MFC
void ::OnNMRClickBacnetTree(NMHDR *pNMHDR, LRESULT *pResult)
{
CPoint ptScreen;
SendMessage(WM_CONTEXTMENU, (WPARAM) m_hWnd, GetMessagePos() );
if (! GetCursorPos(&ptScreen))
{
return ;
}// Select an element under the right click
CPoint ptClient(ptScreen);
m_BACnetTree.ScreenToClient(&ptClient);// Create the right click menu
CMenu rightClickMenu;
rightClickMenu.CreatePopupMenu();
rightClickMenu.AppendMenu(MF_STRING,1021,_T("One"));
rightClickMenu.AppendMenu(MF_STRING,1022,_T("Two"));
rightClickMenu.AppendMenu(MF_STRING,1023,_T("Three"));// Create the sub menu
CMenu rightClickMenuAdvanced;
rightClickMenuAdvanced.CreatePopupMenu();
rightClickMenuAdvanced.AppendMenu(MF_STRING,1024,_T("one"));
rightClickMenuAdvanced.AppendMenu(MF_STRING,1025,_T("two"));// Add the sub menu to the right click menu
if (rightClickMenuAdvanced.GetMenuItemCount() > 0 )
{
rightClickMenu.AppendMenu(MF_POPUP|MF_STRING,(UINT) rightClickMenuAdvanced.m_hMenu, _T("Advanced") );
}
rightClickMenuAdvanced.DestroyMenu();rightClickMenu.TrackPopupMenu (TPM_RIGHTBUTTON, ptScreen.x, ptScreen.y, this);
rightClickMenu.DestroyMenu( );
}
Leave a comment