[Last Call] Learn how to a build a cloud-first strategyRegister Now


popping up a cmenu in the right place

Posted on 2007-08-03
Medium Priority
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

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

Expert Comment

ID: 19625374
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

alb66 earned 2000 total points
ID: 19638048
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

829 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