5

There are at-least 2 ways of doing this AFAIK.

How do I find position of a Win32 control/window relative to its parent window?

and this:

How to get size and location of a control placed on a dialog in MFC?

    htext := GetDlgItem(hDlg, IDI_TEXT);
    GetWindowRect(htext, R);
    // (1)
    // Pt := Point(R.Left, R.Top);
    // ScreenToClient(hDlg, Pt);
    // R := Rect(Pt.X, Pt.Y, Pt.X + R.Right - R.Left, Pt.Y + R.Bottom - R.Top);
    // OR: (2)
    MapWindowPoints(0, {GetParent(htext)} hDlg, R, 2);
    FrameRect(dc, R, brush);

Which of the methods is better and why? Is the method with MapWindowPoints will work with multiple monitors?

My concern is mainly with MapWindowPoints and multi-monitors since passing 0 as hWndFrom will use the HWND_DESKTOP

Community
  • 1
  • 1
kobik
  • 21,001
  • 4
  • 61
  • 121
  • 1
    `MapWindowPoints` takes into account RTL reading whilst `ScreenToClient` doesn't. P.S. maybe you should rephrase the question, someone voted to close it as opinion based. – TLama Nov 07 '14 at 17:21
  • 1
    @TLama, it might be "opinion based", but I need to know why and a proof. You gave a good point about RTL. but I don't see a test case here. will `ScreenToClient` (1) fail where `MapWindowPoints` (2) will not? – kobik Nov 07 '14 at 17:28
  • 1
    If a window has the [`WS_EX_LAYOUTRTL`](http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx#WS_EX_LAYOUTRTL) style, its content is mirrored (you may [`try it`](http://pastebin.com/MXU2W2Lr)). In such case, the `MapWindowPoints` will correctly return mirrored point, whilst `ScreenToClient` a point as if there were no mirror. That's why I would prefer `MapWindowPoints`. – TLama Nov 07 '14 at 17:33
  • @TLama, You are correct about `WS_EX_LAYOUTRTL`. `ScreenToClien` failed. what about multiple monitors?. – kobik Nov 07 '14 at 17:44
  • 2
    Multiple monitors are fine. With both of them. – Sertac Akyuz Nov 07 '14 at 19:33

1 Answers1

3

Since @TLama is refusing to take credit and post an answer I'll post one for him to finalize it. (Thanks! :))


The most obvious problem with method (1) ScreenToClien is that it fails if the Dialog window has the WS_EX_LAYOUTRTL style and its content is mirrored.

In such case, Method (2) MapWindowPoints will correctly return mirrored point.

I could not find any other differences other than WS_EX_LAYOUTRTL.

Both methods works just fine with multiple monitors.

kobik
  • 21,001
  • 4
  • 61
  • 121