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

后端 未结 2 571
遥遥无期
遥遥无期 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条回答
  •  闹比i
    闹比i (楼主)
    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 :)

提交回复
热议问题