Using CMenu::TrackPopupMenu as an Immediate Function

It is possible to make the TrackPopupMenu method of CMenu return the value of the item that was selected rather than having to process the menu commands through the message map. This is possible using the two flags TPM_NONOTIFY and TPM_RETURNCMD. As their names imply the first causes the menu not to send a command massage, and the second causes the method to return the ID of the command selected. If no command was selected the value returned is 0.


CMenu menu;
int pMenuID = 0;
DWORD SelectionMade;
VERIFY(menu.LoadMenu(IDR_MENU1));

CMenu* pPopup = menu.GetSubMenu(pMenuID);
ASSERT(pPopup != NULL);

POINT pp;
GetCursorPos(&pp);
SelectionMade = pPopup->TrackPopupMenu(
	TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
	pp.x,pp.y,this);

pPopup->DestroyMenu();

//The value of SelectionMade is
//the id of the command selected
//or 0 if no selection was made

switch(SelectionMade)
{
	// ..... do something here .....
}

Last updated: 18 April 1998



Comments

  • problem with menu MF_DISABLED don't work

    Posted by Legacy on 11/10/2003 12:00am

    Originally posted by: Javier

    Helleo I create a popup menu in an MFC activeX control but I can't disable an item? Why it dose't work?
    :


    int CAGDCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (COleControl::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here

    // carga el menu
    m_pmenu = new CMenu();
    m_pmenu->CreatePopupMenu();
    m_pmenu->AppendMenu(MF_STRING | MF_DISABLED, IDM_VEREV, (LPCTSTR)"&Ver Evento...");
    m_pmenu->AppendMenu(MF_STRING | MF_GRAYED, IDM_MODIFEV, (LPCTSTR)"&Editar Evento...");
    m_pmenu->AppendMenu(MF_STRING | MF_GRAYED, IDM_BORRAREV, (LPCTSTR)"&Borrar Evento...");
    m_pmenu->AppendMenu(MF_SEPARATOR, 0, "");
    m_pmenu->AppendMenu(MF_STRING | MF_GRAYED, IDM_INFO, (LPCTSTR)"&Info...");
    return 0;
    }


    this method don't work, and this donens't work

    (este otro metodo tampoco funciona:

    m_menu.EnableMenuItem(IDM_MODIFEV, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

    why?

    Reply
  • How to capture which menu item has been selected?

    Posted by Legacy on 10/07/2002 12:00am

    Originally posted by: kws

    Hi,

    Anyone know how can I capture the event when user select on an item on the menu list?

    I need to do some other operation after the user has made selection. Each different selection will run different operation.

    Thanks lot for the time and input from all you guys

    KwS

    Reply
  • Help me a lot ~ Thank~

    Posted by Legacy on 09/26/2002 12:00am

    Originally posted by: Leaves

    Thank You ~~

    Reply
  • YOU DA MAN !!

    Posted by Legacy on 07/12/2002 12:00am

    Originally posted by: M'ster

    Dude.... you are sooooo the man !! what would have taken me few hours I got done in 3 1/2 mins !!! You go !!!

    Reply
  • Great!

    Posted by Legacy on 07/11/2002 12:00am

    Originally posted by: Nemesyzz

    Great - Thank You Very Much!!!

    Reply
  • problem with TPM_RETURNCMD

    Posted by Legacy on 05/11/2002 12:00am

    Originally posted by: Govind

    I used your code to show popup menu on right click on a list control. The handlers for the menu commands were not at all called.
    Then I tried with TPM_RETURNCMD value commented in TrackPopupMenu() function and it worked from then.
    I dont know how this fixed though.

    Reply
  • Tons of thanks...

    Posted by Legacy on 03/20/2002 12:00am

    Originally posted by: shanthu

    Most powerful things are often Simple.

    This made some of my work lot simpler.

    Reply
  • Thanks for addding a step MickySoft left out.

    Posted by Legacy on 01/22/2002 12:00am

    Originally posted by: hankaaron

    The one part of the microsoft's example that they leave out is how to get the current cursor position. When you use the list control window for a pop-up menu, the message handler does not give you the mouse co-ordinates(very odd that the C++ team decided to do this). I had forgotton about the GetCurPos function- that solved the problem. Again Thanks!

    Reply
  • A Great fix for a real neasty bug!

    Posted by Legacy on 01/21/2002 12:00am

    Originally posted by: Giyoar Sharaby

    I used the conventional menu which contained a very large list of items. For some reasone the menu sent me the wrong item command - only on the items last items on the list. It looks like it mis-calculated the items height and sent the above item command.
    This method fix the problem.
    Thanks again.
    Giyora, Israel

    Reply
  • Problem displaying menu!

    Posted by Legacy on 05/18/2001 12:00am

    Originally posted by: diya

    Could you please help?

    I am trying to display a menu using the code below:

    CMenu menu;
    POINT pp;
    DWORD selection;

    VERIFY(menu.LoadMenu(IDR_MENU1));

    // HWND m_hwnd = pNMHDR->hwndFrom;
    // CWnd* myWnd = FromHandle(m_hwnd);

    GetCursorPos(&pp);

    selection = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, pp.x+2,pp.y+2, this);

    menu.DestroyMenu();

    The menu is effectively displayed but only a narrow section, the rest is not visible. What could be the problem?

    Thank you. Hope to hear from you soon!

    Diya


    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's enterprise datacenter can be one of the most complex business environments with dozens (for smaller business), hundreds (for larger and midsize business), even thousands (for hyperscale businesses) of servers that must be managed and monitored. At this level, just managing the cords can be challenging — let alone keeping up with the growing need for more agility and scalability within the datacenter. Simply put, companies are aggressively looking for less complexity and more agility from their …

  • Entire organizations suffer when their networks can't keep up and new opportunities are put on hold. Waiting on service providers isn't good business. In these examples, learn how to simplify network management so that your organization can better manage costs, adapt quickly to business demands, and seize market opportunities when they arise.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date