Export (0) Print
Expand All

WinFormIntegration Sample: Demonstrates Hosting a WinForms User Control in an MFC Application 

Download sample

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:

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 WinForms user control.

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

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

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 .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 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 WinForms by selecting the Save Changes drop-down button in the WinForms StatusStrip. Selecting the MFC CViewCmds->Command1 menu item will update the text in the StatusStrip demonstrating MFC command routing to the hosted WinForms UserControl.

    2. 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.

    3. 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.

Requirements

Supported Operating Systems: Windows Server 2003, Windows XP

See Also

Community Additions

ADD
Show:
© 2014 Microsoft