The following procedures show how to create a .NET Frameworks 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 tabindex="0" keywords="9cff3c56-7564-416b-b9a4-71a9254ed755"/>CView Class and tabindex="0" keywords="d597e397-6529-469b-88f5-7f65a6b9e895"/>CWinFormsView Class.
For information about how to create a Windows Forms user control and author a control class library, see tabindex="0" keywords="79c9cf05-5ab6-4a18-886d-88a64748b098"/>How to: Author User Controls.
In some cases, WinForms 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
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.
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.
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 tabindex="0" keywords="698f8603-457f-435a-baff-5ac9243d6ca1"/>/FU compiler option so that the program will compile; it also copies WindowsControlLibrary1.dll into theMFC02
project directory so that the program will run.In stdafx.h, find this line:
Copy Code
#endif // _AFX_NO_AFXCMN_SUPPORT
Add these lines above it:
Copy Code
#include <afxwinforms.h> // MFC Windows Forms support
Modify the view class so that it inherits from tabindex="0" keywords="d597e397-6529-469b-88f5-7f65a6b9e895"/>CWinFormsView.
In MFC02View.h, replace tabindex="0" keywords="9cff3c56-7564-416b-b9a4-71a9254ed755"/>CView with tabindex="0" keywords="d597e397-6529-469b-88f5-7f65a6b9e895"/>CWinFormsView so that the code appears as follows:
Copy Code
class CMFC02View : public CWinFormsView { };
If you want add additional views to your MDI application, you will need to call tabindex="0" keywords="9bc1fc63-1cff-4301-b8e9-c4bdda3c8fea"/>CWinApp::AddDocTemplate for each view you create.
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:
Copy Code
IMPLEMENT_DYNCREATE(CMFC02View, CWinFormsView) CMFC02View::CMFC02View(): CWinFormsView(WindowsControlLibrary1::UserControl1::typeid) { } BEGIN_MESSAGE_MAP(CMFC02View, CWinFormsView) //leave existing body as is END_MESSAGE_MAP()
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.