Help doing context menus

Posted on 2003-03-03
Medium Priority
Last Modified: 2013-11-20
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.
Question by:EriC
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 11

Accepted Solution

KurtVon earned 150 total points
ID: 8064715
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.

LVL 11

Expert Comment

ID: 8065136
Oops, or if there is a menu on the main window you can call AfxGetMainWnd()->GetMenu().

Author Comment

ID: 8067409
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.
LVL 11

Expert Comment

ID: 8067618
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.


Author Comment

ID: 8068757
thanks for your help

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question