Dialog pos relative to screen

How can I move a dialog to a screen relative position. MoveWindow says this in MSDN.

"The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window's client area. "

I don't want to use paren't client area!

Thanky for any help.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, if you know the UL coordinates of the screen (GetDesktopWindow()) and the UL coordinates of the parent window (GetWindowRect()), it's seems pretty straightforward to move a child window relative to the desktop.

Perhaps a reminder of your basic vector math.  Let's say that the UL of the desktop is point A.  The UL of the parent window is B.  You want to locate the child window at point C.

So to move it there, you want it at vector AC.  But you can move relative to A, only B.  But you know what vector AB is, right.  So then AC = AB + BC.  Since the relative move is BC, the answer is BC = AC - AB.

In other words, subtract the location of the parent window from the desired location of the child window and move it to the resultant.
TSMITHAuthor Commented:
Yes I'm quite aware of this method, it's how I do it at the moment. I was actually looking for a cleaner solution, I'm only interested in a non-botch method.

What's a UL co-ordinate?


Why don't you just use CWnd::ScreenToClient() to calculate the position relative to the parent window from the wanted position relative to the screen?

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

a small sample:

 int x, y; // lets say these are the given screen coordinate for the dialog positioning
 CRect rect;
 GetWindowRect( rect );
 rect.SetRect( x, y, rect.Width(), rect.Height() );
 CWnd* pParent = GetParent();
 // perhaps you need some error testing here with pParent
 pParent->ScreenToClient( &rect );
 MoveWindow( rect );

hope that helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Janusz CzopowikSenior Software EngineerCommented:
This will move your dialog relatively to a screen coordinates:

      int ixCrnr = 100, iyCrnr = 50;  //x and y coordinates of the top-left corner of the dialog
      CRect wndRect;

      MoveWindow(ixCrnr, iyCrnr, wndRect.Width(), wndRect.Height());

Now, JohnCz, this only works for top-level windows (even dialogs can be top-level windows), because the MoveWindow moves the window relative to the parent window which is the desktop's window, but not for child-windows, i.e. a non-modal child dialog within another dialog ... I do not know what TSMITH's problem is, but he asked for a solution which is not depending on the parent's client area as your solution is...
TSMITHAuthor Commented:
Zoppo, yours worked first.
TSMITHAuthor Commented:
Cheers Zoppo
you're welcome              :)
Janusz CzopowikSenior Software EngineerCommented:
TSMITH in his question stated:
How can I move a dialog to a screen relative position. MoveWindow says this in MSDN.

It is known by definition that child windows are contained in the parent’s client area and that’s how windows tick.

From his/her question one could not draw a conclusion that it was about windows with WS_CHILD attribute set.
But, he also said:

>I don't want to use paren't client area!

and that's exactly what MoveWindow() does ...

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.