Click to See Complete Forum and Search --> : CMenu basics


panza
June 8th, 1999, 03:55 PM
Hi,

Does anyone have any good examples on how to use CMenu, dealing with the WM_COMMAND
etc.? Thank you very much.



-.-.-

Alvaro
June 8th, 1999, 10:15 PM
I don't have any samples but it's possible that you're trying to solve a simple problem in a rather complex fashion.

If you could post exactly what you're trying to accomplish related to menus and WM_COMMAND, then perhaps we'll be able to give you the simplest solution that you can implement with MFC.

Alvaro

panza
June 9th, 1999, 02:06 AM
Okay, I've done this, whenever the user hits the right mousebutton:


CMenu *menu=new CMenu();
menu->CreatePopupMenu();
menu->AppendMenu(MF_STRING, 0, "Hallo");
menu->AppendMenu(MF_STRING, 1, "Hallo2");
menu->AppendMenu(MF_STRING, 2, "Hallo3");

POINT p;
GetCursorPos(&p);
menu->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, p.x, p.y, this, NULL




And I've added a line to the message map ON_COMMAND(0, OnHallo), but OnHallo is never called.
I've also tried another ID, like creating an ID called ID_HALLO with a value of 33000, but it didn't work
either - of course the OnHallo methode exists.

What am I doing wrong?

Thank you.


-.-.-

ALM
June 9th, 1999, 08:10 AM
Well, I don't quite know how to solve your problem given the way you're dynamically adding items to the menu.

What I usually do is create the popup menu as a resource (through the resource editor) to look something like this:

POPUP (IDR_FLOATING_MENU)
------
Hallo (ID_HALLO)
Hallo2 (ID_HALLO2)
Hallo3 (ID_HALLO2)

Then I use the Class Wizard to add the handlers to each item, the same as if it were a regular menu. And to pop it up I use this code:


CMenu menuFloating;
if (!menuFloating.LoadMenu(IDR_FLOATING_MENU))
return 0;

// Draw and track the "floating" popup
CPoint point;
GetCursorPos(&point);
menuFloating.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this, NULL);




So, in essense, if you create the popup menu as a resource, you can take advantage of the Class Wizard to handle your menu items.

Hope this helps!

Simon Rose
June 15th, 1999, 11:01 AM
Not sure if this is what you want to know but: the window you specify as the parent in TrackPopupMenu will receive any chosen menu item messages via it's WM_COMMAND handler, called OnCommand. If I remember correctly, a huge switch statement usually does the job, i.e.

switch(wParam)
{
case ID_MENUITEMHERE:
// do stuff (maybe call a function)
break;
case ID_NEXTITEMHERE:
// do stuff
//break;
}




Hope this helps,

Simon