Link to home
Start Free TrialLog in
Avatar of StanChart
StanChart

asked on

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"?
Avatar of jkr
jkr
Flag of Germany image

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.
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;
    }
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NawalKishore1976
NawalKishore1976

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial