【发布时间】:2011-02-08 20:02:51
【问题描述】:
我有指向带有函数的控件的指针
CWnd* CWnd::GetDlgItem(int ITEM_ID)
所以我有 CWnd* 指向控件的指针,
但在CWnd 类中根本找不到任何方法
检索给定控件的大小和位置。
有什么帮助吗?
【问题讨论】:
-
不是 wnd->GetWindowRect(&rect) 吗?
我有指向带有函数的控件的指针
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->。
【讨论】:
pWnd 指针可能无效。 void 返回值不是问题,因为我没有在任何地方使用返回值。
在直接 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
希望这会有所帮助!快乐编码:)
【讨论】: