Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using WinForms controls in an MFC dialog

0.00/5 (No votes)
20 Dec 2005 1  
This article is a simple introduction to using the CWinFormsControl MFC class to put a Windows Forms control on an MFC dialog.

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

    #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_EDIT1 We'll map this to the MaskedTextBox control
    IDC_EDITMASK CEdit m_Mask
    IDC_BUTTONMASK ON_BN_CLICKED -> OnBnSetMask
    IDC_EDIT_STATUS CEdit 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 :

    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 :

    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.

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

    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.

    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.

    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.

    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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here