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

后端 未结 2 570
遥遥无期
遥遥无期 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 18:53

    In straight MFC/Win32: (Example of 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
    

    Hope this helps! Happy coding :)

    0 讨论(0)
  • 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->.

    0 讨论(0)
提交回复
热议问题