MFC provides the template class cwinformsdialog. You can use this class to host the Windows Forms user control (usercontrol) in the mode or mode-free MFC dialog box ).CwinformsdialogIs derived from the MFC class cdialog, so this dialog box can be started in mode or mode without mode.
For example applications that display windows forms used with MFC, see "MFC and winforms integration" (integration of MFC and winforms ).
Create a new MFC application project.
On the File menu, selectNewAnd then click Project ". In the Visual C ++ folder, select "MFC application ".
In the Name box, enterMfc03And change the "solution" settings to "add SOLUTION ". Click OK ".
In the "MFC Application Wizard", accept all default values and click "finish ". This creates an MFC application with a multi-document interface.
Configuration item.
In Solution Explorer, right-clickMfc03Project node, and then select "properties" from the context menu ". The "property page" dialog box appears.
In the "properties" dialog box, select "general" in the "Configure properties" tree control, and in the "project default" section, set "Public Language Runtime Library support" to "Public Language Runtime Library support (/CLR )". Click OK ".
Add a reference to the. NET control.
In Solution Explorer, right-clickMfc03Project node, and then select "Reference ". On the properties page, click "Add new reference", select "windowscontrollibrary1" (under the "project" tab), and click "OK ". This operation adds a reference in the form of/Fu compiler options for program compilation; it also copies windowscontrollibrary1.dllMfc03Project directory for program running.
Set# Include <afxwinforms. h>Add to stdafx. h existing# IncludeStatement.
AddCdialogThe new class of the subclass.
Right-click the project name and addCdialogThe MFC class of the subclass (named chostforwinform ). Because you do not need resources in the dialog box, you can delete the resource ID (select "resource view", expand the "dialog box" folder, and delete the idd_hostforwinform resource. Delete all references to this ID in the Code .)
In the chostforwinform. h and chostforwinform. cpp filesCdialogReplaceCwinformsdialog <windowscontrollibrary1: usercontrol1>.
Call domodal for the chostforwinform class.
In mfc03.cpp, add# Include "hostforwinform. H".
Before the return statement in cmfc03app: initinstance definition, add:
Chostforwinform m_hostforwinform;
M_hostforwinform.domodal ();
Generate and run this project.
On the "generate" menu, click "generate SOLUTION ".
On the Debug menu, click Start (do not Debug )".
Next, we will add code to monitor the control status on Windows forms from the MFC application.
Add the oninitdialog handler.
The "properties" window (F4) is displayed ). In the Class View, select chostforwinform ". In the "properties" window, select overrides, and in the row where oninitdialog is located, click the left column and select "<add> ". This adds the following to chostforwinform. h:
Copy codevirtual BOOL OnInitDialog();
Define oninitdialog as follows (in chostforwinform. cpp:
Copy codeBOOL CHostForWinForm::OnInitDialog() { CWinFormsDialog<WindowsControlLibrary1::UserControl1>::OnInitDialog(); GetControl()->button1->Click += MAKE_DELEGATE(System::EventHandler, OnButton1); return TRUE;}
Next, add the onbutton1 handler. Add the following lines to the public section of the chostforwinform class in chostforwinform. h:
Copy codevirtual void OnButton1( System::Object^ sender, System::EventArgs^ e );BEGIN_DELEGATE_MAP( CHostForWinForm ) EVENT_DELEGATE_ENTRY( OnButton1, System::Object^, System::EventArgs^ );END_DELEGATE_MAP()
In chostforwinform. cpp, add the following definition:
Copy codevoid CHostForWinForm::OnButton1( System::Object^ sender, System::EventArgs^ e ) { System::Windows::Forms::MessageBox::Show("test");}
Generate and run this project. When you click this button (on a Windows form), the code in the MFC application is run.
Next, we will add code to display the value in the text box on Windows form from the MFC code.
In the public section of the chostforwinform class in chostforwinform. H, add the following declaration:
Copy codeCString m_sEditBoxOnWinForm;
In the dodataexchange definition in chostforwinform. cpp, add the following three lines to the end of the function:
Copy codeif (pDX->m_bSaveAndValidate) m_sEditBoxOnWinForm = CString( GetControl()->textBox1->Text);else GetControl()->textBox1->Text = gcnew System::String(m_sEditBoxOnWinForm);
In the onbutton1 definition in chostforwinform. cpp, add the following three lines to the end of the function:
Copy codethis->UpdateData(TRUE);System::String ^ z = gcnew System::String(m_sEditBoxOnWinForm);System::Windows::Forms::MessageBox::Show(z);
Generate and run this project.