How to: Create the User Control and Host MDI View

The following procedures show how to create a .NET Framework user control, author the user control in a control class library (specifically, a Windows Control Library project), and then compile the project into an assembly. You will then consume the control from an MFC application that uses classes derived from CView Class and CWinFormsView Class.

For information about how to create a Windows Forms user control and author a control class library, see How to: Author User Controls.

Note

In some cases, Windows Forms controls, such as a third-party Grid control, might not behave reliably when hosted in an MFC application. A recommended workaround is to place a Windows Forms User Control in the MFC application and place the third-party Grid control inside the User control.

This procedure assumes that you created a Windows Forms Controls Library project named WindowsControlLibrary1, as per the procedure in How to: Create the User Control and Host in a Dialog Box.

To create the MFC host application

  1. Create a new MFC Application project.

    On the File menu, select New, and then click Project. In the Visual C++ folder, select MFC Application.

    In the Name box, enter MFC02 and change the Solution setting to Add to Solution.Click OK.

    In the MFC Application Wizard, accept all the defaults, and then click Finish. This creates an MFC application with a Multiple Document Interface.

  2. Configure the project.

    In Solution Explorer, right-click the MFC02 project node, and select Properties from the context menu. The Property Pages dialog box appears.

    In the Property Pages dialog box, in the Configuration Properties tree control, select General, then in the Project Defaults section, set Common Language Runtime support to Common Language Runtime Support (/clr). Click OK.

  3. Add a reference to the .NET control.

    In Solution Explorer, right-click the MFC02 project node and select References. In the Property Page, click Add New Reference,select WindowsControlLibrary1 (under the Projects tab), and click OK. This adds a reference in the form of a /FU compiler option so that the program will compile; it also copies WindowsControlLibrary1.dll into the MFC02 project directory so that the program will run.

  4. In stdafx.h, find this line:

    #endif // _AFX_NO_AFXCMN_SUPPORT 
    

    Add these lines above it:

    #include <afxwinforms.h>   // MFC Windows Forms support
    
  5. Modify the view class so that it inherits from CWinFormsView.

    In MFC02View.h, replace CView with CWinFormsView so that the code appears as follows:

    class CMFC02View : public CWinFormsView
    {
    };
    

    If you want add additional views to your MDI application, you will need to call CWinApp::AddDocTemplate for each view you create.

  6. Modify the MFC02View.cpp file to change CView to CWinFormsView in the IMPLEMENT_DYNCREATE macro and message map and replace the existing empty constructor with the constructor shown below:

    IMPLEMENT_DYNCREATE(CMFC02View, CWinFormsView)
    
    CMFC02View::CMFC02View(): CWinFormsView(WindowsControlLibrary1::UserControl1::typeid) 
    {
    }
    BEGIN_MESSAGE_MAP(CMFC02View, CWinFormsView)
    //leave existing body as is
    END_MESSAGE_MAP()
    
  7. Build and run the project.

    In Solution Explorer, right-click MFC02 and select Set as StartUp Project.

    On the Build menu, click Build Solution.

    On the Debug menu, click Start without debugging.

See Also

Other Resources

Hosting a Windows Forms User Control as an MFC View