JavaScript EditorDhtml editor     Free javascript download 

Main Page

The procedure in this topic assumes you are creating a new dialog-based (CDialog Class) MFC project, but you can also add support for a Windows Form control to an existing MFC dialog.

To create the .NET user control

  1. Create a new Visual C# Windows Control Library project named WindowsControlLibrary1.

    From the File menu, select New, then Project. In the Visual C# folder, Select the Windows Control Library icon.

    Accept the default project name of WindowsControlLibrary1 by clicking OK.

    The default name of the .NET control will be UserControl1.

  2. Add child controls to UserControl1.

    In Toolbox, open the All Windows Forms list. Drag a Button control to the UserControl1 design surface.

    Also add a TextBox control.

  3. Change the declarations of the TextBox and Button to public from private in the file UserControl1.Designer.cs.

  4. Build the project.

    From the Build menu, click Build Solution.

    You may want to note the full path with file name of the generated DLL in the Build log because you will enter that information into the MFC application.

To create the MFC host application

  1. Create a new MFC Application project.

    From the File menu, select New, then Project. In the Visual C++ folder, select the MFC Application icon.

    In the Name box, enter MFC01. Change the Solution setting to Add to Solution. Click OK. The MFC Application Wizard appears.

    In the MFC Application Wizard, select Application Type. Choose Dialog based. Accept the remaining defaults and click Finish. This will create an MFC application with an MFC Dialog.

  2. Add a placeholder control to the MFC dialog box.

    Click the Resource View tab. In Resource View, double-click on IDD_MFC01_DIALOG. The dialog resource appears in Resource Editor.

    In Toolbox, open the Dialog Editor list. Drag a Static Text control to the dialog resource. The Static Text control will serve as a placeholder for the .NET Windows Forms control. Resize it to approximately the size of the Windows Forms control.

    Change the ID of the Static Text control to IDC_CTRL1 in the Properties window and change the TabStop property to True.

  3. Configure the project.

    In Solution Explorer, right-click the MFC01 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.

  4. Add a reference to the .NET control.

    In Solution Explorer, right-click the MFC01 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 MFC01 project directory so that the program will run.

  5. In stdafx.h, find this line:

    В CopyCode imageCopy Code
    #endif // _AFX_NO_AFXCMN_SUPPORT 

    Add these lines above it:

    В CopyCode imageCopy Code
    #include <afxwinforms.h>   // MFC Windows Forms support
  6. Add code to create the managed control.

    First, declare the managed control. In MFC01Dlg.h, go to the declaration of the dialog class, and add a data member for the user control in Protected scope as follows:

    В CopyCode imageCopy Code
    class CMFC01Dlg : public CDialog
       // ...
       // Data member for the .NET User Control:
       CWinFormsControl<WindowsControlLibrary1::UserControl1> m_ctrl1;

    Next, provide an implementation for the managed control. In MFC01Dlg.cpp, in the dialog override of CMFC01Dlg::DoDataExchange generated by the MFC Application wizard (not CAboutDlg::DoDataExchange, which is in the same file), add the following code to create the managed control and associate it with the static place holder IDC_CTRL1:

    В CopyCode imageCopy Code
    void CMFC01Dlg::DoDataExchange(CDataExchange* pDX)
       DDX_ManagedControl(pDX, IDC_CTRL1, m_ctrl1);
  7. Build and run the project.

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

    From the Build menu, click Build Solution.

    From the Debug menu, click Start without debugging. You will now see the MFC dialog box displaying the Windows Form control.

See Also

JavaScript EditorDhtml editor     Free javascript download