How to get size and location of a control placed on a dialog in MFC?

后端 未结 2 579
遥遥无期
遥遥无期 2021-02-01 18:32

I\'ve got the pointer to the control with function

CWnd* CWnd::GetDlgItem(int ITEM_ID)

so i\'ve got CWnd* pointer which points to

2条回答
  •  情话喂你
    2021-02-01 19:06

    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 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.

    Note: pDlg above is the dialog. If you're in a member function of the dialog class, just remove the pDlg->.

提交回复
热议问题