Udostępnij za pośrednictwem


WinFormIntegration Sample: Demonstrates Hosting 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 simple Windows Forms user control 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.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

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 via the MFC Save and Open menu items. You can invoke the Save functionality from within 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