Thursday, September 20, 2007

How to Host winforms c# control in dialog based MFC application

To create the .NET user control
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.

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.

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

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
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.

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.

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.

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.

In stdafx.h, find this line:

#endif // _AFX_NO_AFXCMN_SUPPORT
Add these lines above it:

#include // MFC Windows Forms support
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:

class CMFC01Dlg : public CDialog
{
// ...
// Data member for the .NET User Control:
CWinFormsControl 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:

void CMFC01Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_ManagedControl(pDX, IDC_CTRL1, m_ctrl1);
}
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

0 comments: