Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing Controls in MFC

Tags:

c++

mfc

I am writing a program which has two panes (via CSplitter), however I am having problems figuring out out to resize the controls in each frame. For simplicity, can someone tell me how I would do it for a basic frame with a single CEdit control?

I'm fairly sure it is to do with the CEdit::OnSize() function... But I'm not really getting anywhere...

Thanks! :)

like image 910
Konrad Avatar asked Sep 24 '08 14:09

Konrad


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.


2 Answers

A window receives WM_SIZE message (which is processed by OnSize handler in MFC) immediately after it was resized, so CEdit::OnSize is not what you are looking for.

You should add OnSize handler in your frame class and inside this handler as Rob pointed out you'll get width and height of the client area of your frame, then you should add the code which adjusts size and position of your control.

Something like this

void MyFrame::OnSize(UINT nType, int w, int h)
{
    // w and h parameters are new width and height of your frame
    // suppose you have member variable CEdit myEdit which you need to resize/move
    myEdit.MoveWindow(w/5, h/5, w/2, h/2);
}
like image 74
Serge Avatar answered Oct 14 '22 12:10

Serge


When your frame receives an OnSize message it will give you the new width and height - you can simply call the CEdit SetWindowPos method passing it these values.

Assume CMyPane is your splitter pane and it contains a CEdit you created in OnCreate called m_wndEdit:

void CMyPane::OnSize(UINT nType, int cx, int cy)
{
    m_wndEdit.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}
like image 22
Rob Avatar answered Oct 14 '22 11:10

Rob