簡體   English   中英

如何獲取放在MFC對話框中的控件的大小和位置?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM