popping up a cmenu in the right place

Posted on 2007-08-03
Last Modified: 2013-11-20

I have a CDialog, with a CDialog created as a child on top of it, something like:

BOOL CMyDialogParent::OnInitDialog()
     m_pChildDlg = new CMyDialogChild();
     m_pChildDlg->Create(..., );
     m_pChildDlg->MoveWindow(0, 30, 100, 100);

When I handle right clicks in the child dialog, I use ClientToScreen() to convert the clicked point to place a popup menu exactly where the user right clicked - but it is always offset by 30 pixels in the Y direction, exactly as much as it is offset in relation to the parent. How do I get it to popup in the right place?

Also, experts exchange made this insance ask questions qizard updte, now I have to go through like 10 steps to ask a question - is there some way to go back to the 'classic' way, it was much faster?

Question by:DJ_AM_Juicebox
    LVL 86

    Expert Comment

    >>How do I get it to popup in the right place?

    The 30 pixels seem to be the caption size that you have to add. You can obtain that value using 'GetSystemMetrics()' (

    int nYCaptionOffset = GetSystemMetrics(SM_CYCAPTION);

    It might not be the same value on all systems (actually, anyone can change that size in "Appearances"), so I'd not hard-code it.
    LVL 1

    Expert Comment

    From what you've described, it sounds like you're calling your parent dialog's ClientToScreen funtion with the click co-ordinates relative to your child dialog's client area.

    Is the implementation for your child's right-click handler in your parent dialog's class?

    If so you will need to explicitly call your child's ClientToScreen function as follows:

    LVL 19

    Accepted Solution

    Instead of handling right click message, it is best to handle WM_CONTEXTMENU message; it pass directly the right position for your menu:

    void CYourWnd::OnContextMenu( CWnd* pWnd, CPoint point )
      CMenu oMenu;
      VERIFY( oMenu.LoadMenu( IDR_YOUR_MENU ));
      CMenu* pPopup = oMenu.GetSubMenu( 0 );
      pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, GetSafeHwnd() );

    remember to add  the ON_WM_CONTEXTMENU() macro in the message map


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now