Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

mfc

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 the control, but simply can't find any method within CWnd class that will retrieve the size and location of a given control. Any help?

like image 476
dragan.stepanovic Avatar asked Apr 16 '10 13:04

dragan.stepanovic


People also ask

How do I make my MFC dialog resizable?

Solution 1. this->SetWindowPos(NULL,0,0,newWidth,newHeight,SWP_NOMOVE | SWP_NOZORDER); This will change the size of the dialog, but you will need to move and resize the control inside the dialog using the same function but with the CWnd of the control.

How do you make a list control dynamically in MFC?

To create the column(s) of a list control, you can use the CListCtrl::InsertColumn() method. One of its syntaxes is: int InsertColumn(int nCol, const LVCOLUMN* pColumn); The nCol argument is the index of the column that this call will create.

How do I edit dialog box in Visual Studio?

How do I create and use a text box (Edit Control) in a dialog box? Visual Studio calls a text box an Edit control. In the dialog resource click the Toolbox tab and select Edit Control from the list. Use the mouse to click and drag the size and location of the text box.

What is MFC dialog box?

MFC supports both kinds of dialog box with class CDialog . The controls are arranged and managed using a dialog-template resource, created with the dialog editor. Property sheets, also known as tab dialog boxes, are dialog boxes that contain "pages" of distinct dialog-box controls.


2 Answers

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

like image 106
interjay Avatar answered Oct 06 '22 00:10

interjay


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 :)

like image 22
Jason Newland Avatar answered Oct 05 '22 23:10

Jason Newland