[英]How to get size and location of a control placed on a dialog in MFC?
我已经通过函数获得了控件的指针
CWnd* CWnd::GetDlgItem(int ITEM_ID)
所以我有CWnd*
指针指向控件,但根本找不到CWnd
类中将检索给定控件的大小和位置的任何方法。 有帮助吗?
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
然后将它们转换为相对于对话框的客户区域,这通常是您需要的。
注意:上面的pDlg
是对话框。 如果您在对话框类的成员函数中,只需删除pDlg->
。
在直接MFC / Win32中:( WM_INITDIALOG示例)
RECT r;
HWND h = GetDlgItem(hwndDlg, IDC_YOURCTLID);
GetWindowRect(h, &r); //get window rect of control relative to screen
POINT pt = { r.left, r.top }; //new point object using rect x, y
ScreenToClient(hwndDlg, &pt); //convert screen co-ords to client based points
//example if I wanted to move said control
MoveWindow(h, pt.x, pt.y + 15, r.right - r.left, r.bottom - r.top, TRUE); //r.right - r.left, r.bottom - r.top to keep control at its current size
希望这可以帮助! 快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.