I am looking for a good example of how to do a pop-up menu in MFC. I have found this bit of code:

void CMyView::OnRButtonDown(UINT nFlags, CPoint point) 
{
   CView::OnRButtonDown(nFlags, point);

   CMenu* menu_bar = AfxGetMainWnd()->GetMenu();
   CMenu* file_menu = menu_bar->GetSubMenu(0);	
   ASSERT(file_menu);

   file_menu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON, point.x, 
	  point.y, this);
}

But what if my menu is from a seperate menu resource? And how would I go about getting a method from when the user clicks on a menu selection in the pop-up window?

Recommended Answers

All 8 Replies

Every resource item must have a unique ID number, both the resource files in your project and resource files in other DLLs. You can use LoadMenu() to load a menu resource that is not normally part of the project. Also read the links here for how to create a shortcut menu.

Every resource item must have a unique ID number, both the resource files in your project and resource files in other DLLs. You can use LoadMenu() to load a menu resource that is not normally part of the project. Also read the links here for how to create a shortcut menu.

I got the menu appearing now.

But there is a new problem. When I mouse over the menu item the action is taken rather than if I click on the menu item.

I mean, the menu comes up alright but I must be doing something wrong when trying to interpret interpreting the mouse click. I use the OnMenuSelect method but it interprets when I mouse over the menu item, not when I click on it. How do I fix this?

You don't need OnMenuSelect(). Just use ClassWizard to implement event handlers just as you do the menu items in the main menu.

OK but MS Studio 2005 does not have class wizard like I was used to.
But I think I am close.
I have gone to the munu and right clicked and selected "Add Event Handler Wizard" that seems like it should be the one. But lots of optioins are greyed out Including the "Add and Edit" button.

OK, I don't know why the options were greyed out. But I started a new project and it works.

THANKS!

But I am just curious because I don't want to have to rewrite the whole program. What makes the Event Handler Wizard greyed out?

When I am in the resource editor and I right click on a menu item to add an event handler, the wizard comes up but the options to add are disabled and the Add and Edit button is grayed out.

Something else I noticed. IN the class view, if I click to see the properties of a class, the properties also does not come up.

I figured it out.
It had to do with a comment that said something like:
// Call-Back Messages

I had it on the wrong line, when I moved it, it was fixed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.