May 26, 2019
Hot Topics:

MFC 8.0 and Windows Forms Integration, Part I

  • November 2, 2005
  • By Nick Wienholt
  • Send Email »
  • More Articles »

After upgrading and converting the project to a managed application (which takes less than a minute), you need to include the new MFC header file that supports Windows Forms control integration (afxwinforms.h) in stdafx.h. Once this is done, you can add a reference to the control for the solution by selecting the Project | References . menu option, clicking the Add New Reference button, selecting the Browse tab, and navigating to the control's assembly, which is NumericBox.dll in this case. For those new to .NET, adding a reference is the .NET equivalent to including a header file. Figure 2 shows the dialog for adding a reference.

Click here for a larger image.

Figure 2: Adding a Reference to a .NET Assembly

Because the Scribble application already has a control in the correct location, all you need to do is change the type of the control object. Because the original Scribble application uses DDX to bypass the Windows control and instead go straight to an integer (which the Scribble document then accesses directly), adding a new control variable and using DDX to hook this up with the dialog is a simpler modification than replacing the integer member variable. To implement this, add a new member variable to CPenWidthsDlg:

CWinFormsControl<NumericBox::NumericBox> m_cThinWidth;

The DoDataExchange function is modified to the following (new code in italics):

void CPenWidthsDlg::DoDataExchange(CDataExchange*pDX)
   DDX_Text(pDX, IDC_THICK_PEN_WIDTH, m_nThickWidth);
   DDV_MinMaxInt(pDX, m_nThickWidth, 1, 20);
   DDX_ManagedControl(pDX, IDC_THIN_PEN_WIDTH, m_cThinWidth);
   //move the thin pen width value between the managed
   // control and the existing member variable
   if (pDX->m_bSaveAndValidate)
      m_nThinWidth = System::Int32::Parse(m_cThinWidth->Text);
      m_cThinWidth->Text = System::String::Format("{0}",

Notice the new templated type CWinFormsControl, which is used when declaring Windows Forms controls, and the new DDX_ function, which manages the creation and exchange of data between the actual control and the CWinFormsControl member variable. Standard data exchange code also manages the exchange of data between the control and the existing m_nThinWidth variable. Preserving m_nThinWidth meant that none of the other code that accessed this member variable needed to be changed.

The other code change to note is the use of the .NET Framework types Int32 and String, which have excellent parsing and formatting functionality. These types became available as soon as the CLR switch was turned on, which is another key benefit of moving past Visual C++ 6 and into the managed world. With only a couple of lines of new code and a couple of changes in the projects options, you have successfully migrated a very old application to take advantage of a new Windows Forms control and the rich functionality of the .NET Framework.

As the Windows Forms integration with MFC is based on COM and ActiveX technology, a call to the MFC function AfxEnableControlContainer was added to the CScribbleApp::InitInstance function, but this is the only change to the code in the Scribble application not directly related to the control instantiation.

But Wait, There's More!

Next month, Part II will drill into the classes that make Windows Forms controls use so easy.

Download the Code

To download the sample code for this article, click here.

About the Author

Nick Wienholt is an independent Windows and .NET consultant based in Sydney, Australia. He is the author of Maximizing .NET Performance from Apress, and specializes in system-level software architecture and development with a particular focus on performance, security, interoperability, and debugging. Nick can be reached at NickW@dotnetperformance.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date