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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • The operational costs of managing an x86 base are taxing IT budgets, making it difficult to fund and staff new initiatives. Today's IT organization must seek efficiencies in its operations and shift to a more agile infrastructure that's flexible enough to adapt to future changes in the business. Read this Q & A session with Jed Scaramella, research manager for IDC's Enterprise Platforms and Data Center Trends, to learn how the integrated nature of the blade platform delivers critically needed efficiencies …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds