Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / MFC
Tip/Trick

Hosting Windows Forms User Control in MFC Dialogs

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 May 2013CPOL2 min read 48.1K   662   12   23
This tip/trick discusses about hosting a Windows Form user control in an MFC dialog box and View windows.

Introduction

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.

Background

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.
MC++
#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".
C++
// MFCApplication1Dlg.h : header file
//

#pragma once


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

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

// Dialog Data
    enum { IDD = IDD_MFCAPPLICATION1_DIALOG };

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


// Implementation
protected:
    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();
    DECLARE_MESSAGE_MAP()
}; 

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

C++
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_ManagedControl(pDX,IDC_STATIC,m_ctrl);
}  

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

History

  • April 28, 2013 - First published.

License

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


Written By
Software Developer (Senior) BHGE
Germany Germany
I worked as a software engineer and researcher in different countries with a wide range of related projects and engineers from all around the world. I was involved in Oil&Gas, Telecommunication, Transportation, and Semiconductor projects and played various roles such as junior, senior, and lead engineer both in embedded and non-embedded devices and technologies.

During my professional carrier, I was directly involved in designing and maintaining editor, compiler, and interpreter for IEC 611131-3 (PLC programming standard) and fault-tolerant communication layer for distributed automation standard IEC 61499, and many other projects such as DCS (Distributed Control Systems), (SCADA) Supervisory Control and Data Acquisition System, Oilfield (CMS) Computerised Maintenance Systems, Oil&Gas Laboratory Automaton Systems, and Semiconductor Equipment Connectivity Solutions.

Currently, I pursue a Ph.D. degree in Computer Science in the Technical University of Dresden and works as a software engineer in Germany. Beside, I am a certified specialist in Microsoft technologies since 2011.

My main research and work areas are Industrial Communication and Automation Systems, Real-Time Systems, Service-Oriented Systems, IEC 61131-3, IEC 61499, and Distributed Embedded Systems.

Comments and Discussions

 
BugWrong code line Pin
srabbis9-Sep-17 21:26
srabbis9-Sep-17 21:26 
GeneralRe: Wrong code line Pin
lz123411-Sep-18 4:54
lz123411-Sep-18 4:54 
Questioncall Application.Exit() in C#. but it doesnt work Pin
Yincity18-Jan-15 19:59
Yincity18-Jan-15 19:59 
AnswerRe: call Application.Exit() in C#. but it doesnt work Pin
Aydin Homay19-Jan-15 0:21
Aydin Homay19-Jan-15 0:21 
GeneralRe: call Application.Exit() in C#. but it doesnt work Pin
Yincity19-Jan-15 1:09
Yincity19-Jan-15 1:09 
GeneralRe: call Application.Exit() in C#. but it doesnt work Pin
Aydin Homay19-Jan-15 1:24
Aydin Homay19-Jan-15 1:24 
GeneralRe: call Application.Exit() in C#. but it doesnt work Pin
Yincity19-Jan-15 4:30
Yincity19-Jan-15 4:30 
GeneralRe: call Application.Exit() in C#. but it doesnt work Pin
Aydin Homay19-Jan-15 8:36
Aydin Homay19-Jan-15 8:36 
Questiondata transfer between MFC and .NET Pin
Yincity19-Jan-15 4:46
Yincity19-Jan-15 4:46 
AnswerRe: data transfer between MFC and .NET Pin
Aydin Homay19-Jan-15 8:41
Aydin Homay19-Jan-15 8:41 
QuestionMFC to CLR Pin
Member 1045308313-Dec-13 4:11
Member 1045308313-Dec-13 4:11 
QuestionMFC to CLR Pin
Member 1045308312-Dec-13 6:26
Member 1045308312-Dec-13 6:26 
AnswerRe: MFC to CLR Pin
Aydin Homay12-Dec-13 7:54
Aydin Homay12-Dec-13 7:54 
QuestionMFC to CLR Pin
Member 1045308311-Dec-13 7:47
Member 1045308311-Dec-13 7:47 
AnswerRe: MFC to CLR Pin
Aydin Homay11-Dec-13 8:37
Aydin Homay11-Dec-13 8:37 
GeneralRe: MFC to CLR Pin
Member 63608813-Dec-13 5:59
Member 63608813-Dec-13 5:59 
GeneralRe: MFC to CLR Pin
Aydin Homay13-Dec-13 8:11
Aydin Homay13-Dec-13 8:11 
GeneralRe: MFC to CLR Pin
Member 63608813-Dec-13 23:51
Member 63608813-Dec-13 23:51 
GeneralRe: MFC to CLR Pin
Aydin Homay14-Dec-13 7:04
Aydin Homay14-Dec-13 7:04 
BugThere are errors Pin
Member 100130225-Jun-13 19:56
Member 100130225-Jun-13 19:56 
GeneralRe: There are errors Pin
Aydin Homay5-Jun-13 23:30
Aydin Homay5-Jun-13 23:30 
GeneralRe: There are errors Pin
Member 1106563226-Oct-17 3:55
Member 1106563226-Oct-17 3:55 
GeneralRe: There are errors Pin
Aydin Homay28-Nov-17 22:07
Aydin Homay28-Nov-17 22:07 

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

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