popping up a cmenu in the right place


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?

Who is Participating?
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

>>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()' (http://msdn2.microsoft.com/en-us/library/ms724385.aspx):

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.
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:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.