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.

TSMITHAsked:
Who is Participating?
 
ZoppoCommented:
a small sample:

CMyDlg::AnyFoo()
{
 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,

ZOPPO
0
 
jhanceCommented:
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.
0
 
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?

Thankyou.
0
Cloud Class® Course: 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.

 
ZoppoCommented:
Hi TSMITH,

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?

ZOPPO
0
 
Janusz CzopowikCommented:
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;
      GetWindowRect(wndRect);

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

0
 
ZoppoCommented:
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...
0
 
TSMITHAuthor Commented:
Zoppo, yours worked first.
0
 
TSMITHAuthor Commented:
Cheers Zoppo
0
 
ZoppoCommented:
you're welcome              :)
0
 
Janusz CzopowikCommented:
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.
0
 
ZoppoCommented:
But, he also said:

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

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

ZOPPO
0
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.