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;

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

SelectionMade = pPopup->TrackPopupMenu(


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

	// ..... do something here .....

Last updated: 18 April 1998


  • 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->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:



  • How to capture which menu item has been selected?

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

    Originally posted by: kws


    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


  • Help me a lot ~ Thank~

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

    Originally posted by: Leaves

    Thank You ~~

  • 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 !!!

  • Great!

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

    Originally posted by: Nemesyzz

    Great - Thank You Very Much!!!

  • 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.

  • 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.

  • 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!

  • 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

  • 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;


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


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


    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!


  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

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