如何获取放置在 MFC 对话框中的控件的大小和位置? [英] How to get size and location of a control placed on a dialog in MFC?

查看:34
本文介绍了如何获取放置在 MFC 对话框中的控件的大小和位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有指向带有函数的控件的指针

I've got the pointer to the control with function

CWnd* CWnd::GetDlgItem(int ITEM_ID)

所以我有指向控件的 CWnd* 指针,但在 CWnd 类中根本找不到任何方法检索给定控件的大小和位置.有什么帮助吗?

so i've got CWnd* pointer which points to the control, but simply can't find any method within CWnd class that will retrieve the size and location of a given control. Any help?

推荐答案

CRect rect;
CWnd *pWnd = pDlg->GetDlgItem(YOUR_CONTROL_ID);
pWnd->GetWindowRect(&rect);
pDlg->ScreenToClient(&rect); //optional step - see below

//position:  rect.left, rect.top
//size: rect.Width(), rect.Height()

GetWindowRect 给出控件的屏幕坐标.pDlg->ScreenToClient 然后将它们转换为相对于对话框的客户区,这通常是您需要的.

GetWindowRect gives the screen coordinates of the control. pDlg->ScreenToClient will then convert them be relative to the dialog's client area, which is usually what you need.

注意:上面的pDlg是对话框.如果您在对话框类的成员函数中,只需删除 pDlg->.

Note: pDlg above is the dialog. If you're in a member function of the dialog class, just remove the pDlg->.

这篇关于如何获取放置在 MFC 对话框中的控件的大小和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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