Displaying main menu as a popup menu???
Dear All,
I have a main menu in SDI application.. IDR_MAINFRAME.. Which contain File, Edit, View and Help menu. I want to display this main menu by right clicking on the mouse. When I used following code, It is displaying small bar on left side. I couldnt see File, Edit etc menu...
void CMenuView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu* menu_bar = AfxGetMainWnd()->GetMenu();
file_menu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x,
point.y, this);
}
How can I display the main menu as such as a popup menu??
Thanks in advance,
Kareem.
mail : [email protected]
Re: Displaying main menu as a popup menu???
If you want to display the main menu as a popup menu follow these steps:
1. Create a temporary popup menu (menuPopup)
2. Append all popup submenus from the main menu to this menu
3. Call TrackPopupMenu.
4. Don't forget to remove the submenus from the temporary popup menu, otherwise the menuPopup destructor will destroy the submenus and your program will crash when you try to access the main menu again.
Code:
CMenu* pMainMenu = AfxGetMainWnd()->GetMenu();
CMenu menuPopup;
if(menuPopup.CreatePopupMenu())
{
int nSubMenus = pMainMenu->GetMenuItemCount();
for(int i=0;i<nSubMenus;i++)
{
CString s;
pMainMenu->GetMenuString(i,s,MF_BYPOSITION);
menuPopup.AppendMenu(MF_POPUP,(UINT)pMainMenu->GetSubMenu(i)->GetSafeHmenu(),s);
}
menuPopup.TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
for(i=0;i<nSubMenus;i++)
menuPopup.RemoveMenu(0,MF_BYPOSITION);
}
Re: Displaying main menu as a popup menu???
Thank you very much. It is working !! :) :thumb: