Click here to Skip to main content
Click here to Skip to main content
Go to top

Resize MDI Child Frame Windows to Fit Form Views

, 16 Apr 2001
Rate this:
Please Sign up or sign in to vote.
Make the MDI windows which contain your program's forms snap to match the forms' sizes.

 [Sample Image - 25K]

Introduction

Many applications allow the user to input and edit data using forms. If this is done in an MDI environment, managing the sizes of the MDI frame windows in which the forms are displayed can look like a daunting task. This article shows you how to call MFC functions to size the MDI child frame window to fit the form contained in the form view within it.

Resize Your Form With MFC!

Look in your derived form view class for a CFormView::OnInitialUpdate() function override. This is usually inserted for you if you created the form view class with AppWizard or ClassWizard. Go to the location of the implementation of the override in your code. Next, look for a call to ResizeParentToFit() that is on a line by itself. If you find such a call, replace the line with the code shown in bold below. If there is no such call, then add the code shown in bold to the implementation of OnInitialUpdate():

void CMyFormView::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    GetParentFrame()->RecalcLayout();
    ResizeParentToFit(FALSE);
    ...
}

The size of the client area of the view's MDI child frame window should now match the size of the dialog template used to place the controls on the form.

Don't Resize or Maximize!

The sample code provided with this article shows how to resize your form view frame windows to fit their forms, as outlined above. However, you probably don't want the user to be able to resize or maximize the window thereby defeating the purpose of calling CScrollView::ResizeParentToFit(). This is because MFC places the form in the top right corner of the client area of the MDI Child frame window. If you want to prevent the user from resizing or maximizing the frame window, disable the WS_THICKFRAME and WS_MAXIMIZEBOX styles, and add the WS_BORDER style to the window. This can be easily done using the CMDIChildWnd::PreCreateWindow() override placed in your code for you by AppWizard. This is, of course, usually in a CChildFrame class. Here's how the sample does it:

BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    if( !CMDIChildWnd::PreCreateWindow(cs) )
        return FALSE;

    cs.style &= ~(WS_THICKFRAME);
    cs.style &= ~(WS_MAXIMIZEBOX);
 
    cs.style |= WS_BORDER;

    return TRUE;
}

Conclusion

If you have any questions about what I've done with these techniques, please feel free to contact me anytime, or post a message in the message board below!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Brian C Hart
Software Developer (Senior) Corrugated Technologies, Inc.
United States United States
From Fridley, Minnesota and I like computer programming! When I got started, I was working mostly with Windows GUI programming in C/C++. Then later on I worked with COM/DCOM for a school internship. I used COM/DCOM to write an ad hoc cluster server and job-running environment for a cluster of 24 Windows-based high-end visualization workstations. I moved on to C# and have been working in C# and Windows Forms ever since. I have yet to embrace Silverlight Smile | :)
Follow on   Twitter

Comments and Discussions

 
Questiongot my 5 Pinmembermarc ochsenmeier13-Feb-12 20:25 
GeneralMy vote of 5 PinmemberCarlJ19-Dec-11 16:41 
Thanks, It's exactly what I need.
GeneralPlease Vote for this Article! PinmemberBrian Hart29-Dec-05 10:34 
Generalfuncionality about controls PinmemberVichh_15-Jun-04 8:08 
Questionhow manually child frames can put in MDI PinmemberLadwal17-Jul-03 21:23 
AnswerRe: how manually child frames can put in MDI PinmemberBrian Hart18-Jul-03 8:13 
GeneralRe: how manually child frames can put in MDI PinmemberLadwal18-Jul-03 23:00 
GeneralMFC Form View PinmemberSir Chandresque14-Jun-02 8:49 
GeneralIs it possible to remove the _[]X controls ... Pinmemberrbc25-Apr-02 7:11 
GeneralRe: Is it possible to remove the _[]X controls ... PinmemberBrian Hart29-Dec-05 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 17 Apr 2001
Article Copyright 2001 by Brian C Hart
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid