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!
Dr. Brian Hart obtained his Ph.D. in Astrophysics from the University of California, Irvine, in 2008. Under Professor David Buote, Dr. Hart researched the structure and evolution of the universe. Dr. Hart is an Astrodynamicist / Space Data Scientist with Point Solutions Group in Colorado Springs, CO, supporting Space Operations Command, United States Space Force. Dr. Hart is a Veteran of the U.S. Army and the U.S. Navy, having most recently served at Fort George G. Meade, MD, as a Naval Officer with a Cyber Warfare Engineer designator. Dr. Hart has previously held positions at Jacobs Engineering supporting Cheyenne Mountain/Space Force supporting tests, with USSPACECOM/J58 supporting operators using predictive AI/ML with Rhombus Power, and with SAIC supporting the Horizon 2 program at STARCOM. Dr. Hart is well known to the community for his over 150 technical publications and public speaking events. Originally from Minneapolis/Saint Paul, Minnesota, Dr. Hart lives in Colorado Springs with his Black Lab, Bruce, and likes bowling, winter sports, exploring, and swimming. Dr. Hart has a new movie coming out soon, a documentary called "Galaxy Clusters: Giants of the Universe," about his outer space research. The movie showcases the Chandra X-ray Observatory, one of NASA’s four great observatories and the world’s most powerful telescopes for detecting X-rays. The movie has been accepted for screening at the U.S. Air Force Academy ("USAFA" for short) Planetarium and will highlight how scientists use clusters of galaxies, the largest bound objects in the Universe, to learn more about the formation and evolution of the cosmos --- as well as the space telescopes used for this purpose, and the stories of the astronauts who launched them and the scientists who went before Dr. Hart in learning more about the nature of the Universe.