Question on using a CMenu

Hi,

I create a CMenu on the fly when the user right clicks on one of my controls.  It's a simple cut/copy/paste menu.

            CMenu *PopupMenu = new CMenu();
            POINT DesktopPoint;
            DesktopPoint.x = point.x;
            DesktopPoint.y = point.y;
            ClientToScreen(&DesktopPoint);
            PopupMenu->AppendMenu(MF_STRING, 1, "Copy");
            PopupMenu->AppendMenu(MF_STRING, 2, "Cut");
            PopupMenu->AppendMenu(MF_STRING, 3, "Paste");
            BOOL bResponse =
                  PopupMenu->TrackPopupMenu(TPM_LEFTALIGN, DesktopPoint.x, DesktopPoint.y, this);

The above code shows the menu but how do I know if they chose "Copy", "Cut", or "Paste"?
StanChartAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
... or, for your purposes:

          CMenu *PopupMenu = new CMenu();
         POINT DesktopPoint;
         DesktopPoint.x = point.x;
         DesktopPoint.y = point.y;
         ClientToScreen(&DesktopPoint);
         PopupMenu->AppendMenu(MF_STRING, 1, "Copy");
         PopupMenu->AppendMenu(MF_STRING, 2, "Cut");
         PopupMenu->AppendMenu(MF_STRING, 3, "Paste");
         int  nResponse =
               PopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RETURNCMD, DesktopPoint.x, DesktopPoint.y, this);

    switch (nResponse)
   {
   case 1 : // Copy
       break;
   case 2 : // Cut
       break;
   case 3 : // Paste
       break;
    }
0
 
jkrCommented:
The window you're passing in as the last parameter of 'TrackPopupMenu()' will receive the selction as a 'WM_COMMAND' message with

wNotifyCode = HIWORD(wParam); // notification code - 0 for menus
wID = LOWORD(wParam);         // item, control, or accelerator identifier - the menu item ID
hwndCtl = (HWND) lParam;      // handle of control

So, you'll receive 1, 2 or 3 in wID.
0
 
jkrCommented:
Oh, there even seems to be an easier way using 'TPM_RETURNCMD', see http://www.codeproject.com/menu/MenusForBeginners.asp#tpm

    // some typical menu code using the TPM_RETURNCMD option:
    CMenu menu;
    CMenu *pSub = NULL;
   
    // popup a menu to get the number of pages to display
    VERIFY(menu.LoadMenu(IDR_PREVIEW_PAGES));
    pSub = menu.GetSubMenu(0);
   
    // NOTE : If you need to enable or disable the menu items
    //  in this list based on the number of pages in your printout,
    // you can either do it here before the menu is displayed, or write a
    // handler for the WM_INITMENUPOPUP message and configure the
    // enabled/disabled state at that point.
    int command = pSub->TrackPopupMenu(
            TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD,
            point.x,
            point.y,
            this);
   
    switch (command)
    {
    case ID_PAGES_1PAGE :
        m_Across = 1;
        m_Down = 1;
        m_nZoomOutPages = 1;
        break;
    case ID_PAGES_2PAGES :
        m_Across = 2;
        m_Down = 1;
        m_nZoomOutPages = 2;
        break;
    case ID_PAGES_3PAGES :
        m_Across = 3;
        m_Down = 1;
        m_nZoomOutPages = 3;
        break;
    case ID_PAGES_4PAGES :
        m_Across = 2;
        m_Down = 2;
        m_nZoomOutPages = 4;
        break;
    case ID_PAGES_6PAGES :
        m_Across = 3;
        m_Down = 2;
        m_nZoomOutPages = 6;
        break;
    case ID_PAGES_9PAGES :
        m_Across = 3;
        m_Down = 3;
        m_nZoomOutPages = 9;
        break;
    default :
        return;
    }
0
 
NawalKishore1976Commented:
I will suggest to create one Menu in Resource.

On Right Click Load and Display this menu.

You can eaisly add the message handler using the MFC class wizard then.
0
 
NawalKishore1976Connect With a Mentor Commented:
CMenu menu;
 VERIFY(menu.LoadMenu(IDR_MENU1));
 CMenu* pPopup = menu.GetSubMenu(0);
 ASSERT(pPopup != NULL);
 
 pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, AfxGetMainWnd());
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.