WinFormIntegration Sample: Demonstrates Hosting a WinForms User Control in an MFC Application
This sample demonstrates how to host a WinForms user control as a dialog or a view in an MFC application.
The sample is comprised of three MFC extension DLLs, two WinForms user controls, and one main MFC MDI app:
EXTDLL2 – Shows how to host a WinForms user control as a static control inside an existing CView window.
WinFormUserControl1 – A simple WinForms user control with some common controls added.
WinFormUserControlView1 – A WinForms user control that implements the Microsoft::VisualC::MFC::IView and Microsoft::VisualC::MFC::ICommandTarget interfaces to participate in MFC command routing and View notifications. This is a basic text editor (using a multiline .NETcontrol) that uses MFC to save and open files.
MFC04 – This is the main MFC MDI app that loads the MFC extension DLLs, adding them as different MFC doc templates.
To build and run the sample:
To build the projects, simply open MFC04.sln and select Build Solution from the Build menu.
To run the different scenarios, launch the app and from the File menu, select New. Then select the CView, CFormView, or Dialogs template:
The CView template opens a CWinFormsView with a .NET multiline editbox that you can edit. You also have the ability to save this text into a file and load it again via the MFC Save and Open menu items. You can invoke the Save functionality from within the WinForms by selecting the Save Changes drop-down button in the WinForms. Selecting the MFC CViewCmds->Command1 menu item will update the text in the StatusStrip demonstrating MFC command routing to the hosted WinForms UserControl.
The CFormView template is the same WinForms UserControl as above except it is hosted inside of a CFormView resource. This template only demonstrates the hosting code, the Save and Open functionality is not implemented.
The Dialogs template is a View demonstrating a WinForms UserControl hosted in the following scenarios: inside of a CDialog invoked modal, inside of a CDialog invoked modeless, as a CWinFormsDialog invoked modal, and as a CWinFormsDialog invoked modeless. The CWinFormsDialog examples demonstrate hooking MFC functions up to WinForms control events. These examples also show how to pass strings from managed back to native via the CString to System::String^ conversion operators.
Supported Operating Systems: Windows Server 2003, Windows XP