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

x
  • 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
     m_pPoint=point;

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


}

any help would be wonderful, thanks.
0
EriC
Asked:
EriC
  • 3
  • 2
1 Solution
 
KurtVonCommented:
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

    m_mMenu.LoadMenu(IDM_WINDOW);
    SetMenu(&m_mMenu);

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.


0
 
KurtVonCommented:
Oops, or if there is a menu on the main window you can call AfxGetMainWnd()->GetMenu().
0
 
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.
0
 
KurtVonCommented:
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:

MENUITEMINFO info;
newMenu.CreateMenu()
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.

0
 
EriCAuthor Commented:
thanks for your help
0

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