【问题标题】:Which is the best way to find position of a control relative to it's parent window?找到控件相对于其父窗口的位置的最佳方法是什么?
【发布时间】:2015-01-04 13:27:17
【问题描述】:

至少有 2 种方法可以执行此 AFAIK。

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

还有这个:

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);

哪种方法更好,为什么? MapWindowPoints 的方法是否适用于多台显示器?

我关心的主要是MapWindowPoints 和多显示器,因为将0 传递为hWndFrom 将使用HWND_DESKTOP

【问题讨论】:

  • MapWindowPoints 考虑 RTL 读数,而 ScreenToClient 没有。附:也许你应该改写这个问题,有人投票结束它是基于意见的。
  • @TLama,它可能是“基于意见的”,但我需要知道原因和证明。你对 RTL 给出了一个很好的观点。但我在这里没有看到测试用例。 ScreenToClient (1) 会失败,而 MapWindowPoints (2) 不会?
  • 如果一个窗口有WS_EX_LAYOUTRTL样式,它的内容是镜像的(你可以try it)。在这种情况下,MapWindowPoints 将正确返回镜像点,而ScreenToClient 将返回一个点,就好像没有镜像一样。这就是为什么我更喜欢MapWindowPoints
  • @TLama,您对WS_EX_LAYOUTRTL 的看法是正确的。 ScreenToClien 失败。多台显示器呢?
  • 多个显示器都可以。和他们两个。

标签: c++ delphi winapi mfc dialog


【解决方案1】:

由于@TLama 拒绝承认并发布答案,我会发布一个让他完成。 (谢谢!:))


(1)ScreenToClien 方法最明显的问题是,如果 Dialog 窗口具有 WS_EX_LAYOUTRTL 样式并且其内容被镜像,则它会失败。

在这种情况下,方法(2)MapWindowPoints将正确返回镜像点。

除了WS_EX_LAYOUTRTL,我找不到任何其他区别。

这两种方法都适用于多台显示器。

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 2013-03-13
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多