我在 Visual Studio 2008 C++ 中工作。我有一个 MFC 对话框,里面有一个控件。我正在尝试在控件中放置另一个对话框。
第二个对话框中的 SetWindowPos() 显然使用了屏幕坐标,因此我需要获取控件或父对话框的屏幕坐标。 MSDN 文档说 GetWindowRect() 提供“相对于显示屏左上角的屏幕坐标”,但这不是我得到的。在控件上,它给出了相对于父级的坐标。在父级上,它给出 left=0 和 top=0。我也尝试了 GetWindowPlacement() 中的矩形,它给出了同样的结果。一切都与 parent 有关。
为什么 GetWindowRect() 不返回屏幕相对坐标?有没有其他方法可以获取它们?
我不是编程新手,但对 Windows 编程、Visual Studio 和 MFC 相当陌生,所以我可能遗漏了一些明显的东西。
这是我在 OnInitDialog 中为父对话框所做的:
// 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 包含:
left=0 top=0 right=297 bottom=400
最佳答案
OnInitDialog
在显示对话框之前由框架调用。此时,最终大小和位置都未知:
Windows sends the WM_INITDIALOG message to the dialog box during the Create, CreateIndirect, or DoModal calls, which occur immediately before the dialog box is displayed.
对话框的最终大小和位置是窗口定位协商的结果。发送到此信息可用的对话框的第一条消息是 WM_WINDOWPOSCHANGED
.使用 MFC,此消息通过 CWnd::OnWindowPosChanged
处理.可以通过覆盖 CDialog
派生类中的 OnWindowPosChanged
来实现自定义处理代码。
关于c++ - GetWindowRect 坐标不是屏幕相关的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26220252/