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
Create a fresh MFC dialog based application using VC++ 2005. And turn on /clr compilation.
Add the following header file include to your stdafx.h
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.
|We'll map this to the |
CEdit m_StatusEdit (DDX)
Table 1 : The controls that you need to add to the dialog resource
CWinFormsControl variable to your dialog header file :
CWinFormsControl class provides the functionality to host a .NET control in an MFC application.
In your dialog class's
DoDataExchange, add a call to
void CDialogFormsDlg::DoDataExchange(CDataExchange* pDX)
DDX_ManagedControl(pDX, IDC_MASKED_EDIT1, m_MaskedEdit);
. . .
This creates the .NET control and associates it with that resource ID.
Add the event handler declaration your header file.
Setup the delegate map in a public section of your dialog class declaration.
BEGIN_DELEGATE_MAP( CDialogFormsDlg )
EVENT_DELEGATE_ENTRY( OnMaskInputRejected, System::Object^,
The delegate map allows us to use an MFC class function as a delegate by calling
MAKE_DELEGATE on it.
m_MaskedEdit->Mask = gcnew System::String(strMask);
MaskedTextBox control in
m_MaskedEdit->PromptChar = L' ';
OnMaskInputRejected function to the dialog class.
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.");
m_StatusEdit.SetWindowText(L"Bad entry. Check your input!");
That's it. We are all done. Build and run the application. What you see is an MFC dialog that contains a WinForms control.
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.
- Dec 20, 2005 - First published.
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com
where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket
as well as a programming book – Extending MFC applications with the .NET Framework
Nish's latest book C++/CLI in Action
published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.