MFC and WinForms Integration

This Visual C++ 2005 sample demonstrates how to host a WinForm user control as a dialog or a view in an MFC application.
  • Version:


    File Name:


    Date Published:


    File Size:

    241 KB

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

      The sample is comprised of three MFC extension dlls, two WinForm 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 WinForm user control as a static control inside an existing CView window.

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

      WinFormUserControl1 – A simple WinForm user control with some common controls added.

      WinFormUserControlView1 – A WinForm 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.
  • Supported Operating System

    Windows Server 2003, Windows XP

      Visual Studio 2005 July CTP or later.
      (Please note that this sample does not work with Visual Studio 2005 Beta 2)
    • To build the projects simply open MFC04.sln in Visual Studio 2005 and select Build Solution from the Build menu.

      To run the different scenarios, launch the app and go to File->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 WinForm by selecting the “Save Changes” drop-down button in the WinForm StatusStrip. Selecting the MFC CViewCmds->Command1 menu item will update the text in the StatusStrip demonstrating MFC command routing to the hosted WinForm UserControl.

      The CFormView template is the same WinForm 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 WinForm UserControl hosted in the following scenarios: inside of a CDialog invoked modal, inside of a CDialog invoked modeless, as a CWinFormsDialog invoked modal, as a CWinFormsDialog invoked modeless. The CWinFormsDialog examples demonstrate hooking MFC functions up to WinForm control events. These examples also show how to pass strings from managed back to native via the CString to System::String^ conversion operators.
Site feedback

What category would you like to give web site feedback on?

Rate your level of satisfaction with this web page today: