How to use the CFormView class in single document interface (SDI) and multiple document interface (MDI) applications

Article translations Article translations
Article ID: 98598 - View products that this article applies to.
This article was previously published under Q98598
Expand all | Collapse all

On This Page

Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

Note Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SUMMARY

The CFormView class provides a convenient method to place controls into a view that is based on a dialog box template. The general procedure to use a CFormView is described in the documentation for the class and is illustrated in the VIEWEX and CHKBOOK sample applications provided with Microsoft Foundation Classes (MFC) versions 2.x and above. However, these applications do not demonstrate making the initial size of the frame window to be the same as the initial size of the form.

The following section lists the steps required to support creating a single document interface (SDI) or multiple document interface (MDI) application based on a CFormView, sizing the initial frame window around the form, changing the style of the frame, and closing an MDI document using a button in the form.

MORE INFORMATION

The following steps describe how to create an MFC AppWizard application using the CFormView as the default view:
  1. Use the AppWizard to generate an SDI or MDI application. This inserts a dialog box template with the correct styles set for your project's resource file.

    Visual Studio 6.0:

    At step 6 of the AppWizard, select the view class. In the Base class combo box, select CFormView to specify CFormView as the base class.

    Visual Studio .NET or Visual Studio 2005:

    In step 1 of the AppWizard, select Generated Classes. In the Base class combo box, select CFormView to specify CFormView as the base class.
  2. Override the OnUpdate() member function and call UpdateData() as documented in the CFormView documentation to update the member variables with the current document data and to perform dialog data exchange (DDX).

    NOTE: UpdateData is not virtual and calling the base class ensures that the derived class DoDataExchange is called through standard polymorphism. The CFormView documentation states to call, not override UpdateData.
  3. If you would like to set the initial size of the form view, override the OnInitialUpdate() function. The text below provides additional information about this step, which is slightly different in an SDI or MDI application.

Changing the Size of an SDI Main Frame Around a CFormView

To change the size of the main frame of an SDI application (that uses CFormView as its view class) to be the appropriate size for the form you designed in App Studio, override the OnInitialUpdate() function in your class derived from CFormView, as follows:
      void CMyFormView::OnInitialUpdate()
      {
         CFormView::OnInitialUpdate();
         GetParentFrame()->RecalcLayout();
         ResizeParentToFit(); // default argument is TRUE
      }
				
The ResizeParentToFit() function does not prevent the form from changing size when the user changes the size of the application main frame (scroll bars are added automatically if needed). To modify the style of the frame window that is the parent of a form view, you can override the PreCreateWindow() function in the CMainFrame class generated by AppWizard. For example, to remove the WS_THICKFRAME style and prevent the user from changing the size of the window, declare PreCreateWindow() in MAINFRM.H and add the following code to MAINFRM.CPP:
      BOOL CMainFrame::PreCreateWindow(CREATESTRUCT &cs)
      {
         cs.style &= ~WS_THICKFRAME;
         return CFrameWnd::PreCreateWindow(cs);
      }
				

Changing the Size of an MDI Child Frame Around a CFormView

The process of changing the size of an MDI child frame is similar to changing the size of a main frame for an SDI application, as explained above. However, the RecalcLayout() call is not required.

To change the size of an MDI child frame around a form view, override the OnInitialUpdate() function in your class derived from CFormView as follows:
      void CMDIFormView::OnInitialUpdate()
      {
         CFormView::OnInitialUpdate();
         ResizeParentToFit(); // Default argument is TRUE.
      }
				
If the application overrides the default argument to the ResizeParentToFit() function, essentially the same consequences occur as for an SDI application, as explained above. In addition, the child window may be too large for the enclosing MDI main frame or for the entire screen.

To change the style of the MDI child frame (for example, to remove the WS_THICKFRAME style so the user cannot change the size of the window), derive an MDI child window class and override the PreCreateWindow function as demonstrated in the SDI example above.

Closing an MDI Form with a Button

To create a button on a form that closes the document add a message handler for the BN_CLICKED message to the CFormView class. Make sure that the buttons in CFormView do not have the default IDOK or IDCANCEL identifiers. If they do, then incorrect entries in the message map and incorrect functions for the buttons will be created.

Once the message handler is in place, you can simulate the Close command on the File menu with the following code:
      void CMyForm::OnClickedButton1()
      {
         PostMessage(WM_COMMAND, ID_FILE_CLOSE);
      }
				
This method to close a form prompts the user to save the file if the IsModified() member function associated with the document returns TRUE.

Properties

Article ID: 98598 - Last Review: January 5, 2006 - Revision: 4.0
APPLIES TO
  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.51
    • Microsoft Visual C++ 1.52 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 5.0 Standard Edition
    • Microsoft Visual C++ 6.0 Service Pack 5
    • Microsoft Visual C++ .NET 2003 Standard Edition
    • Microsoft Visual C++ .NET 2002 Standard Edition
    • Microsoft Visual C++ 2005 Express Edition
Keywords: 
kbhowto kbuidesign kbdocview kbmdi KB98598

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com