我已经获得了指向函数的控件的指针。
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
返回值不是问题,因为我没有在任何地方使用返回值。 - interjay在传统的 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