?
Solved

Question on using a CMenu

Posted on 2005-05-04
5
Medium Priority
?
611 Views
Last Modified: 2013-11-20
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"?
0
Comment
Question by:StanChart
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13930493
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
 
LVL 86

Expert Comment

by:jkr
ID: 13930533
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
 
LVL 86

Accepted Solution

by:
jkr earned 1600 total points
ID: 13930558
... 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
 
LVL 3

Expert Comment

by:NawalKishore1976
ID: 13936315
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
 
LVL 3

Assisted Solution

by:NawalKishore1976
NawalKishore1976 earned 400 total points
ID: 13936347
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question