Share via


WinFormIntegration Sample: Shows How to Host a Windows Forms User Control in an MFC Application

This sample demonstrates how to host a Windows Forms user control as a dialog or a view in an MFC application.

The sample is comprised of three MFC extension DLLs, two Windows Forms user controls, and one main MFC MDI app:

EXTDLL1 – Uses CWinFormsView to host the WinFormUserControlView1 user control as a CView window inside the MFC MDI app. Also uses command routing to send messages to the Windows Forms user control.

EXTDLL2 – Shows how to host a Windows Forms user control as a static control inside an existing CView window.

EXTDLL3 – Encapsulates the different ways a Windows Forms user control can be hosted in an MFC dialog, including using CDialog and CWinFormsDialog.

WinFormUserControl1 – A basic Windows Forms user control together with some common controls added.

WinFormUserControlView1 – A Windows Forms 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 .NET TextBox control) 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.

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

To build and run the sample:

  1. To build the projects, simply open MFC04.sln and select Build Solution from the Build menu.

  2. To run the different scenarios, launch the app and from the File menu, select New. Then select the CView, CFormView, or Dialogs template:

    1. 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 by using the MFC Save and Open menu items. You can invoke the Save functionality from the Windows Forms by selecting the Save Changes drop-down button in the Windows Forms StatusStrip. Selecting the MFC CViewCmds->Command1 menu item will update the text in the StatusStrip demonstrating MFC command routing to the hosted Windows Forms UserControl.

    2. The CFormView template is the same Windows Forms 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.

    3. The Dialogs template is a View demonstrating a Windows Forms 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 Windows Forms control events. These examples also show how to pass strings from managed back to native via the CString to System::String^ conversion operators.

Requirements

Supported Operating Systems: Windows Server 2003, Windows XP

See Also

Reference

MFC Windows Forms Samples