Thread: pop up menu problem

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    247

    pop up menu problem

    Hi folks,
    I have a problem with a program I have constructed as a lesson in Sams Visual C++ in 21 days. The problem is with the method for the event handler for a pop up menu and the function OnContextMenu. The program compiles and links okay, and works as per the book up until I right click anywhere in the dialog to display the pop up menu. The menu appears not where the mouse was clicked, but further down 45 degrees to the right of the mouse.
    Does anyone know what is wrong. The code is as in the book...?
    Code:
    void CMenusDlg::OnContextMenu(CWnd* pWnd, CPoint point) 
    {
    	// declare local variables
    	CMenu *m_lMenu;		// pointer to the menu
    	CPoint m_pPoint;		// copy of the mouse position
    
    	// copy the mouse position to a local variable
    	m_pPoint = point;
    	// convert position to a screen position
    	ClientToScreen(&m_pPoint);
    	// get a pointer to the window menu
    	m_lMenu = GetMenu();
    	// get a pointer to the first submenu
    	m_lMenu = m_lMenu->GetSubMenu(0);
    	// show the Pop Up Menu
    	m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON,
    			m_pPoint.x, m_pPoint.y, this, NULL);
    
    }
    system is Win98SE and compiler is msvc++ v.6.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Looks like a problem with the mouse coods.

    Try using GetCursorPos() instead of the ClientToScreen() (or just comenting out the ClientToScreen )

    I suspect the coods are already in screen orientation and converting them again is causing the menu to appear in the wrong place.

    If you still have a problem please post how you got the mouse coods.
    "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

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    From msdn for CWnd::OnContextMenu:
    afx_msg void OnContextMenu( CWnd* pWnd, CPoint pos );

    pos Position of the cursor, in screen coordinates, at the time of the mouse click.
    Try using ScreenToClient, instead of ClientToScreen.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Umm.......

    TrackPopupMenu() takes screen coods. OnContextMenu sends them as screen coods so why use a conversion at all?
    (as I said I thought they might have been 'converted' twice)

    One thing I also just noticed,

    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

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Originally posted by novacain
    Umm.......

    TrackPopupMenu() takes screen coods. OnContextMenu sends them as screen coods so why use a conversion at all?
    (as I said I thought they might have been 'converted' twice)
    A good point, well made. And shame on me: I never read that far down in the code. Sorry for any confusion I may have caused.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    247

    Looks like a problem with the mouse coods.

    Try using GetCursorPos() instead of the ClientToScreen() (or just comenting out the ClientToScreen )

    I suspect the coods are already in screen orientation and converting them again is causing the menu to appear in the wrong place.
    Thanks novacain, looks like that was the problem, commenting out the ClientToScreen worked, obviously they may have been converted twice.

    Thanks guys for the help.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack problem: print after pop function
    By ivan12yu2000 in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2009, 02:18 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Window - Menu problem...
    By FromHolland in forum Windows Programming
    Replies: 1
    Last Post: 02-26-2004, 03:49 PM
  4. MDI and MENU Problem
    By Marc in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2004, 06:59 PM
  5. Problem implementing POP
    By Unregistered in forum Linux Programming
    Replies: 6
    Last Post: 07-02-2002, 04:28 AM