如何获取放置在 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->.
相关文章