Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Help doing context menus

Here is the code i have right now and when i compile and run the application, and right click anywhere it comes up as an error.

void CMenusDlg::OnContextMenu(CWnd* pWnd, CPoint point)
     // TODO: Add your message handler code here

//declare vairables
     CMenu *m_lMenu; //pointer to menu
     CPoint m_pPoint;  // copy of mouse position

     //copy mouse position

     //Convert the position to a screen position
     //Get a pointer to the window menu
     m_lMenu - GetMenu();
     //Get a pointer to first submenu
     //show popup menu
     m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON, m_pPoint.x, m_pPoint.y, this, NULL);


any help would be wonderful, thanks.
  • 3
  • 2
1 Solution
Well, for one thing, make sure the window has a menu.  Dialogs don't have menus, and neither do child windows, so the function GetMenu() will return NULL.  Even if you know the window has a menu, it's probably best to check to see if GetMenu() returned NULL before getting a submenu anyway.

If so there are three solutions I see.  The first is to create a menu and set it into the dialog. Add to the OnInitDialog


where m_mMenu is a CMenu object.

Or, if you don't want to have the window display a menu (likely if it is a dialog) load the menu from a resource instead of calling GetMenu().

Or, if by "window menu" you meant the system menu, call GetSystemMenu();  Though every system menu can be different, so don't assume the first element is a submenu.

Hope this helps.

Oops, or if there is a menu on the main window you can call AfxGetMainWnd()->GetMenu().
EriCAuthor Commented:
Thank you, one more question tho, the menu that it showed was the hole menu and not just one section of it, how can i get it to show just one section of the menu.
Do you mean a submenu, or just some menu items from the submenu?

If you want to display an entire submenu you can use the GetSubMenu function like you did in teh sample code, but be safe and,unless you have complete control over the menu, check to see if it is a submenu first.

To display just a few items from the menu, create a CMenu, the go through the menu items one by one picking out the ones you want:

info.cbSize = sizeof(info);
info.cch =

/* Loop to find the items you want */
oldMenu->GetMenuItemInfo(index, &item, TRUE);
newMenu.AppendMenu(item.dwTypeData, item.wID, oldMenu->GetItemString(index));


I think you get the idea.

EriCAuthor Commented:
thanks for your help

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now