GetWindowRect 坐标不是屏幕相关的 [英] GetWindowRect coordinates not screen-relative

查看:30
本文介绍了GetWindowRect 坐标不是屏幕相关的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2008 C++ 中工作.我有一个 MFC 对话框,里面有一个控件.我正在尝试在控件中定位另一个对话框.

I am working in Visual Studio 2008 C++. I have an MFC dialog with a control inside it. I am trying to position another dialog in the control.

第二个对话框上的SetWindowPos() 显然是在使用屏幕坐标,所以我需要获取控件或父对话框的屏幕坐标.MSDN 文档说 GetWindowRect() 提供相对于显示屏左上角的屏幕坐标",但这不是我得到的.在控件上,它提供相对于父级的坐标.在父级上,它给出 left=0 和 top=0.我也尝试过 GetWindowPlacement() 中的矩形,它给出了同样的结果.一切都与父级相关.

SetWindowPos() on the second dialog is clearly using screen coordinates, so I need to get the screen coordinates of the control or the parent dialog. The MSDN documentation says GetWindowRect() provides "screen coordinates relative to the upper-left corner of the display screen" but this is NOT what I am getting. On the control it gives coordinates relative to the parent. On the parent it gives left=0 and top=0. I have tried the rectangle from GetWindowPlacement() as well and it gives the same thing. Everything is relative to the parent.

为什么 GetWindowRect() 不返回屏幕相对坐标?有没有其他方法可以得到它们?

Why is GetWindowRect() not returning screen-relative coordinates? Is there another way to get them?

我对编程并不陌生,但对 Windows 编程、Visual Studio 和 MFC 相当陌生,所以我可能会遗漏一些明显的东西.

I'm not new to programming, but fairly new to Windows programming, Visual Studio, and MFC, so I may be missing something obvious.

这是我在 OnInitDialog 中为父对话框所做的:

Here is what I am doing in OnInitDialog for the parent dialog:

// TestApp message handlers

BOOL TestApp::OnInitDialog()
{
    CDialog::OnInitDialog();

    FILE * pFile = fopen("out.txt","w");
    CRect winRect;
    GetWindowRect(&winRect);
    fprintf(pFile,"left=%li top=%li right=%li bottom=%li\n",winRect.left,winRect.top,winRect.right,winRect.bottom); fflush(pFile);
    fclose(pFile);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

运行时,对话框不会出现在屏幕的左上角,但 out.txt 包含:

When run, the dialog does NOT appear at the upper-left corner of the screen, but out.txt contains:

left=0 top=0 right=297 bottom=400

推荐答案

如另一个答案中所述:

OnInitDialog 在窗口移动到其最终位置之前被调用.如果稍后调用 GetWindowRect,您将看到它返回正确的坐标.

OnInitDialog is called before the window is moved to its final position. If you call GetWindowRect later you'll see it return the proper coordinates.

只需将 PostMessage 与 WM_APP+n 消息一起使用.该消息会在消息泵运行时到达,当窗口定位并显示在屏幕上时该消息会到达.

Just use PostMessage with a WM_APP+n message. This message will arrive when the message pump is running and the message will arrive when the window is positioned and shown on the screen.

或者使用计时器.这具有相同的效果.

Or use a timer. This has the same effect.

这篇关于GetWindowRect 坐标不是屏幕相关的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆