Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / C++/CLI
Article

Using WinForms controls in an MFC dialog

Rate me:
Please Sign up or sign in to vote.
4.88/5 (31 votes)
20 Dec 2005CPOL2 min read 294.6K   4.3K   77   62
This article is a simple introduction to using the CWinFormsControl MFC class to put a Windows Forms control on an MFC dialog.

Image 1

Introduction

A few days ago, someone asked on the Code Project VC++ forum whether he can use the Dundas WinForms controls on his MFC dialogs. The answer is - yes, that's entirely possible, and pretty easy to do with VC++ 2005.

This article is a simple introduction to using the CWinFormsControl MFC class to put a Windows Forms control on an MFC dialog. The article demonstrates this using a small dialog based application that has a .NET MaskedTextBox control placed on it. The MaskInputRejected event is handled through a function declared in the MFC dialog class.

Steps to put the .NET control in the MFC dialog

  1. Create a fresh MFC dialog based application using VC++ 2005. And turn on /clr compilation.

  2. Add the following header file include to your stdafx.h

    MC++
    #include <afxwinforms.h>
  3. Modify your dialog resource so it has the controls shown in the screenshot above, with one change. Where you see the MaskedTextBox control in the screenshot, put a static control. You should end up with the following controls.

    IDC_MASKED_EDIT1We'll map this to the MaskedTextBox control
    IDC_EDITMASKCEdit m_Mask
    IDC_BUTTONMASKON_BN_CLICKED -> OnBnSetMask
    IDC_EDIT_STATUSCEdit m_StatusEdit (DDX)
    IDC_STATIC"Set Mask"
    IDC_STATIC"Enter Text"
    Table 1 : The controls that you need to add to the dialog resource
  4. Add the CWinFormsControl variable to your dialog header file :

    MC++
    CWinFormsControl<System::Windows::Forms::MaskedTextBox> m_MaskedEdit;

    The CWinFormsControl class provides the functionality to host a .NET control in an MFC application.

  5. In your dialog class's DoDataExchange, add a call to DDX_ManagedControl :

    MC++
    void CDialogFormsDlg::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);
        DDX_ManagedControl(pDX, IDC_MASKED_EDIT1, m_MaskedEdit);
    . . .
    }

    This creates the .NET control and associates it with that resource ID.

  6. Add the event handler declaration your header file.

    MC++
    void OnMaskInputRejected(System::Object^, 
        System::Windows::Forms::MaskInputRejectedEventArgs^);
  7. Setup the delegate map in a public section of your dialog class declaration.

    MC++
    BEGIN_DELEGATE_MAP( CDialogFormsDlg )
        EVENT_DELEGATE_ENTRY( OnMaskInputRejected, System::Object^, 
            System::Windows::Forms::MaskInputRejectedEventArgs^ )
    END_DELEGATE_MAP()

    The delegate map allows us to use an MFC class function as a delegate by calling MAKE_DELEGATE on it.

  8. Setup the OnBnSetMask function.

    MC++
    void CDialogFormsDlg::OnBnSetMask()
    {
       CString strMask; 
       m_Mask.GetWindowText(strMask);
       m_MaskedEdit->Clear();
       m_MaskedEdit->Mask = gcnew System::String(strMask);
    }
  9. Setup the MaskedTextBox control in OnInitDialog.

    MC++
    m_MaskedEdit->PromptChar = L' ';
    m_Mask.SetWindowText(L"00/00/0000");
    OnBnSetMask();
    m_MaskedEdit->MaskInputRejected +=
        MAKE_DELEGATE( System::Windows::Forms::MaskInputRejectedEventHandler, 
        OnMaskInputRejected);
  10. Add the OnMaskInputRejected function to the dialog class.

    MC++
    void CDialogFormsDlg::OnMaskInputRejected(System::Object^, 
        System::Windows::Forms::MaskInputRejectedEventArgs^ args)
    {
      if(m_MaskedEdit->MaskFull)
      {
        m_StatusEdit.SetWindowText(L"You've hit the max length of the mask.");
      }
      else if(args->Position == m_MaskedEdit->Mask->Length)
      {
        m_StatusEdit.SetWindowText(L"You are at the end of the mask.");
      }
      else
      {
        m_StatusEdit.SetWindowText(L"Bad entry. Check your input!");
      }  
    }
  11. That's it. We are all done. Build and run the application. What you see is an MFC dialog that contains a WinForms control.

Conclusion

This should help you reuse your existing MFC applications and at the same time use new hyper looking .NET UI controls that are released in the market. Please use the article forum to provide feedback about the article or to post any questions you may have.

History

  • Dec 20, 2005 - First published.

License

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


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralRe: Lockup problem using CWinFormsControl Pin
Jerry Jeremiah10-Sep-08 15:53
Jerry Jeremiah10-Sep-08 15:53 
Generalproblems with embedding .NET control in header file when calling MFC application is not compiled with /clr Pin
raghu venkataraman9-May-06 7:26
raghu venkataraman9-May-06 7:26 
GeneralRe: problems with embedding .NET control in header file when calling MFC application is not compiled with /clr Pin
Nish Nishant12-Jul-06 10:21
sitebuilderNish Nishant12-Jul-06 10:21 
QuestionDatagrid in MFC Dialog Pin
Vanathi.P2-Apr-06 20:47
Vanathi.P2-Apr-06 20:47 
AnswerRe: Datagrid in MFC Dialog Pin
Kulch4212-Jun-06 11:02
Kulch4212-Jun-06 11:02 
GeneralRe: Datagrid in MFC Dialog Pin
ddillon200513-May-07 19:40
ddillon200513-May-07 19:40 
AnswerRe: Datagrid in MFC Dialog Pin
Rolf Kristensen4-Sep-08 1:17
Rolf Kristensen4-Sep-08 1:17 
GeneralSemi Blank Control Pin
tahngarth31-Mar-06 12:05
tahngarth31-Mar-06 12:05 
QuestionMFC project to Form base project Pin
msonawane9-Jan-06 13:35
msonawane9-Jan-06 13:35 
AnswerRe: MFC project to Form base project Pin
Nish Nishant10-Jan-06 3:30
sitebuilderNish Nishant10-Jan-06 3:30 
GeneralRe: MFC project to Form base project Pin
msonawane10-Jan-06 12:59
msonawane10-Jan-06 12:59 
GeneralNice! Pin
Majid Shahabfar27-Dec-05 4:59
Majid Shahabfar27-Dec-05 4:59 
GeneralRe: Nice! Pin
Nish Nishant29-Dec-05 5:37
sitebuilderNish Nishant29-Dec-05 5:37 
GeneralMicrosoft has provided the easy way again Pin
Aamir Butt22-Dec-05 23:35
Aamir Butt22-Dec-05 23:35 
GeneralRe: Microsoft has provided the easy way again Pin
Nish Nishant23-Dec-05 1:09
sitebuilderNish Nishant23-Dec-05 1:09 
GeneralArticle #100 Pin
Jörgen Sigvardsson21-Dec-05 10:21
Jörgen Sigvardsson21-Dec-05 10:21 
GeneralRe: Article #100 Pin
Nish Nishant21-Dec-05 10:32
sitebuilderNish Nishant21-Dec-05 10:32 
GeneralRe: Article #100 Pin
ThatsAlok21-Dec-05 17:50
ThatsAlok21-Dec-05 17:50 
GeneralRe: Article #100 Pin
Nish Nishant22-Dec-05 0:45
sitebuilderNish Nishant22-Dec-05 0:45 
GeneralOpenEx function problem Pin
mhar20-Dec-05 22:20
professionalmhar20-Dec-05 22:20 
GeneralRe: OpenEx function problem Pin
Nish Nishant21-Dec-05 0:55
sitebuilderNish Nishant21-Dec-05 0:55 
GeneralRe: OpenEx function problem Pin
mhar21-Dec-05 13:23
professionalmhar21-Dec-05 13:23 
GeneralRe: OpenEx function problem Pin
Nish Nishant22-Dec-05 2:14
sitebuilderNish Nishant22-Dec-05 2:14 
GeneralReverse... Pin
Imtiaz Murtaza20-Dec-05 16:53
Imtiaz Murtaza20-Dec-05 16:53 
GeneralRe: Reverse... Pin
Nish Nishant21-Dec-05 0:56
sitebuilderNish Nishant21-Dec-05 0:56 

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.