Konrad
Konrad

Reputation: 40947

Resizing Controls in 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! :)

Upvotes: 15

Views: 47053

Answers (8)

db-hopper
db-hopper

Reputation: 17

It is better to use the Dynamic Layout capabilities of each control at the Property section.

Let's say you want to have a specific control, like a heading, always at the center of the view/dialog, then you just choose the properties of Dynamic Layout of the control, Moving Type as Horizontal and Moving X as 50 but you keep sizing to None. This way, when you resize the view, the header remains always at the center. You have to keep in mind that the minimum of the resizing/moving is the size/position of the control within the dialog/view, when you designed it at the Resource View.

This way, you save the burden of geometry and the transformations.

Upvotes: 0

Serge
Serge

Reputation: 7694

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);
}

Upvotes: 11

nUOs
nUOs

Reputation: 31

When it comes to the window size changes, there are three window messages you may be interested in: ON_WM_SIZE(), ON_WM_SIZING(), and ON_WM_GETMINMAXINFO().

As the official docs says:

  • ON_WM_SIZE whose message handler is ::OnSize() is triggered after the size of the CWnd has changed;
  • ON_WM_SIZING whose message handler is ::OnSizing() is triggered when the size of the client area of the clipbord-viewer window has changed;
  • ON_WM_GETMINMAXINFO whose message handler is ::OnGetMinMaxInfo() is triggered whenever the window needs to know the maximized position or dimensions , or the minimum or maximum tracking size.

If you want to restrict the size of the cwnd to some range, you may refer to message ON_WM_GETMINMAXINFO; and if you want to retrieve the size changes in real time, you may refer to the other two messages.

Upvotes: 0

Sergey Kornilov
Sergey Kornilov

Reputation: 1792

I use CResize class from CodeGuru to resize all controls automatically. You tell how you want each control to be resized and it does the job for you.

The resize paradigm is to specify how much each side of a control will move when the dialog is resized.

SetResize(IDC_EDIT1, 0,   0,   0.5, 1);
SetResize(IDC_EDIT2, 0.5, 0,   1,   1);

Very handy when you have a large number of dialog controls.

Source code

Upvotes: 1

Brian Ensink
Brian Ensink

Reputation: 11218

Others have pointed out that WM_SIZE is the message you should handle and resize the child controls at that point. WM_SIZE is sent after the resize has finished.

You might also want to handle the WM_SIZING message which gets sent while the resize is in progress. This will let you actively resize the child windows while the user is still dragging the mouse. Its not strictly necessary to handle WM_SIZING but it can provide a better user experience.

Upvotes: 2

Rob
Rob

Reputation: 78648

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);
}

Upvotes: 10

Eddie
Eddie

Reputation: 13735

GetDlgItem(IDC_your_slidebar)->SetWindowPos(...) // actually you can move ,resize...etc

Upvotes: 5

ravenspoint
ravenspoint

Reputation: 20462

SetWindowPos is a little heavy duty for this purpose. MoveWindow has just what is needed.

Upvotes: 3

Related Questions