Thread: popup menu in MFC?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    Question popup menu in MFC?

    I'm trying to get a popup menu working in my dialog app, I'm almost directly copying the code from a VC++ E-book that I got from a link on these forums, but I've run into a problem

    the code :

    void CAboutDlg::OnContextMenu(CWnd* pWnd, CPoint point)
    {
    CMenu *m_lMenu; // A pointer to the menu
    CPoint m_pPoint; // A copy of the mouse position

    m_pPoint = point; // Copy mouse position to a local variable
    ClientToScreen(&m_pPoint); // Convert to an absolute screen position
    m_lMenu - GetMenu(); //Get a pointer to the window menu
    m_lMenu = m_lMenu->GetSubMenu(0); //Get a pointer to the first submenu
    m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_RIGHTBUTTON, m_pPoint.x, m_pPoint.y, this, NULL);//show the popup menu
    }

    the problem comes on the line

    m_lMenu - GetMenu(); //Get a pointer to the window

    I get the following warning

    warning C4552: '>>' : operator has no effect; expected operator with side-effect

    then a complaint that m_lMenu is being used on the next line without being initialized

    Although the app compiles and runs OK, there is no popup menu functionality

    Thanks

    Merry christmas to all those who help !

  2. #2
    Unregistered
    Guest
    Dude, change your line to

    m_IMenu = GetMenu();
    or
    m_IMenu->GetMenu();

    I don't know MFC so I am not sure what the correct call is. I suspect the first one is the correct one. Not everything you see in books is correct!

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    ok, changed it to m_lMenu = GetMenu(); and it compiles without warnings or errors, although the popup menu still isnt working?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    Sorry :(

    I made a horribly stupid mistake when creating the OnContextMenu function, I made it a member function of the CAboutDlg Class instead of CMainDlg, damn MFC, its too easy to click the wrong buttons

    I still have another problem though. Although the menu now appears it's way out of place, the menu appears in the right place when the dialog is in the top left corner, but if I move the dialog to the middle of the screen, the menu will appear in the bottom right corner, so it seems that I'm not sending the right mouse positions to TrackPopupMenu(); If it makes any difference I'm on Windows XP Pro in 1600x1200.

    the code:
    void CDay6Dlg::OnContextMenu(CWnd* pWnd, CPoint point)
    {
    CMenu *m_lMenu; // A pointer to the menu
    CPoint m_pPoint; // A copy of the mouse position

    m_pPoint = point; // Copy mouse position to a local variable
    ClientToScreen(&m_pPoint); // Convert to an absolute screen position
    m_lMenu = GetMenu(); //Get a pointer to the window menu
    m_lMenu = m_lMenu->GetSubMenu(0); //Get a pointer to the first submenu
    m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON, m_pPoint.x, m_pPoint.y, this, NULL);//show the popup menu
    }

    As I understand it the ClientToScreen() converts the mouse position within the application to the overall position on the screen, and then I have given this as an argument to the TrackPopupMenu function in the form of m_pPoint.x and m_pPoint.y, shouldnt this put the menu in the right place? Can anybody help?

    thanks

    Shouldnt be programming on Christmas eve, but I cant help it.
    Last edited by Robert602; 12-24-2001 at 12:51 PM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    ?

    I seem to have fixed the problem by simply not using the ClientToScreen() function, deleting this line makes the menu appear in the right place? No idea why, I thought the mouse position given was that relative to the dialog, not the screen, isnt that what ClientToScreen is for? Must be mistaken. Sorry for this thread it has been a bit pointless, but if anyone can offer an explanation as to why I dont need ClientToScreen I'd be happy to hear it.

    thanks again

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Just one little hint: don't prefix your local variables with "m_". It's a widely accepted prefix for class members and it gets really confusing reading your code then.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    1

    Cool FIXED Only 6 years later!

    Heh, I happen to be learning from the same book, I spent a long time Googling for the answer to this evil problem and I found Sams' corrections. it's weird how it's just randomly in a FAQ in their book store.

    The answer is:

    • Line 18, "m_lMenu - GetMenu();" should be "m_lMenu = GetMenu();".
    • Line 23, "m_pPoint.x, m_pPoint.y" should be "point.x, point.y".
    Found at: http://www.samspublishing.com/bookst...409&rl=1#info3

    I'm going to keep this FAQ nearby for the duration of this book in case there are more bugs later on.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Robert602 View Post
    m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON, m_pPoint.x, m_pPoint.y, this, NULL);//show the popup menu
    TPM_CENTERALIGN + TPM_LEFTBUTTON

    should be

    TPM_CENTERALIGN | TPM_LEFTBUTTON
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Popup menu
    By rEtard in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2005, 01:59 AM
  4. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM