Click here to Skip to main content
Click here to Skip to main content

Hosting Windows Forms User Control in MFC Dialogs

, 7 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip/trick discusses about hosting a Windows Form user control in an MFC dialog box and View windows.


Sometimes we need to use integration legacy systems with a new technology. In this tip/trick, I will discuss about hosting .NET Windows Form user controls in an MFC dialog box so we can develop our legacy systems in MFC with .NET and related components after that.


In the past if you wanted to use .NET components in your MFC application, you could use COM technology and it would be terrible. But after Visual Studio 2005, Microsoft introduced the C++/CLI technology. With this new technology, developers can use C# or VB.NET and any other language supported with .NET in MFC projects.

Using the Code

First of all, we need to open two projects, MFC and Windows Form User Control Library, so:

  1. Open Visual Studio preferably Visual Studio 2012.
  2. Select File/New Project/Visual C++ and then select MFC application project template.
  3. Click next and select dialog based in MFC application wizard window and at the finally 'Finish'.
  4. Right click on project and select properties. Later, you should change Common Language Runtime Support to Common Language Runtime Support (/clr).
  5. Build your project
  6. Add the following header file include to your stdafx.h.
#include <afxwinforms.h> 

Now, we need to open the second project:

  1. From the Solution Explorer window, right click on the MFC project solution and select Add/New Project item.
  2. Select other language/Visual C#/Windows/Windows Forms Control Library.
  3. Set the project name WinFormUserControl and then press OK.
  4. Right click on the C# project and click properties, change Target Framework to .NET 4 if it is .NET 4.5.
  5. Right click on the MFC project and click properties, Common Properties/Add New Reference/ Add your C# project DLL from here.
  6. Add User Control in your project.
  7. Open your dialog box header file "MFCApplication1Dlg.h" and define CWinFormsControl member with this name: "m_ctrl".
// MFCApplication1Dlg.h : header file

#pragma once

// CMFCApplication1Dlg dialog
class CMFCApplication1Dlg : public CDialogEx
    CWinFormsControl<WinFormUserControl::UserControl1> m_ctrl;

// Construction
    CMFCApplication1Dlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data

    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();

After that you should go to MFCApplication1Dlg.cpp and override the DoDateExchange method:

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

Now it is finished and you can run your project. For more information, click here.


  • April 28, 2013 - First published.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) KTC(Kerman Tablo Co.)-Industrial Automation
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I am an eternal student.
Follow on   Google+   LinkedIn

Comments and Discussions

QuestionMFC to CLR PinmemberMember 1045308313-Dec-13 5:11 
QuestionMFC to CLR PinmemberMember 1045308312-Dec-13 7:26 
AnswerRe: MFC to CLR PinprofessionalAYDIN EBRAHIMI HOMAY12-Dec-13 8:54 
QuestionMFC to CLR PinmemberMember 1045308311-Dec-13 8:47 
AnswerRe: MFC to CLR PinprofessionalAYDIN EBRAHIMI HOMAY11-Dec-13 9:37 
GeneralRe: MFC to CLR PinmemberMember 63608813-Dec-13 6:59 
GeneralRe: MFC to CLR PinprofessionalAYDIN EBRAHIMI HOMAY13-Dec-13 9:11 
GeneralRe: MFC to CLR PinmemberMember 63608814-Dec-13 0:51 
GeneralRe: MFC to CLR PinprofessionalAYDIN EBRAHIMI HOMAY14-Dec-13 8:04 
BugThere are errors PinmemberMember 100130225-Jun-13 20:56 
GeneralRe: There are errors PinmemberAYDIN EBRAHIMI HOMAY6-Jun-13 0:30 
Please check the C# project properties and be sure target framework on 4.0 not 4.5 that should be work Wink | ;)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 7 May 2013
Article Copyright 2013 by AYDIN EBRAHIMI HOMAY
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid