Application Security Testing: An Integral Part of DevOps
This article will showcase the technique to host WPF content in an MFC-based application. The main usage of this implementation is to enhance the graphical appearance of conventional Win32/MFC-based applications. The demo program that comes along with this article will host an animated WPF clock in an MFC dialog program.
Development Tools & Library
- MS Visual Studio 2005 or above
- MS .NET Framework 3.0 or above (download)
The .NET Framework 3.0 supported only in the following platforms:
- Windows Vista
- Windows XP SP2
- Windows Server 2003 SP1
If you are a C++ developer and want to use WPF as an add-on to enhance the graphical UI of your existing Win32/MFC applications, this article is written for you. To get the most out from this article, you should be familiar with VC++/CLi, MFC, XAML, and C#. But, if you don't know C# or XAML, you can still proceed just by referencing the generated DLL and make use of it in your MFC application.
What Is WPF?
WPF stands for Windows Presentation Foundation. It's a subsystem of Microsoft .NET Framework 3.0. It allows developers to productively create visually appealing applications and improve user experience. The deployment of WPF enables richer control, design, and development of the visual aspects of Windows programs. It aims to unify a host of application services: user interface, 2D and 3D drawing, fixed and adaptive documents, advanced typography, vector graphics, animation, data binding, audio, and video. WPF also provides a consistent programming model for building applications and provides a clear separation between the UI and the business logic.
WPF APIs are managed code, but most existing Win32/MFC programs are coded in unmanaged C++. Conventionally, WPF APIs cannot be called from a true unmanaged program. However, by using the /clr option with the VC++ compiler, you can create a mixed managed-unmanaged program where you can mix managed and unmanaged API calls seamlessly.
One thing to note is that you are not allowed to compile XAML files into a C++ project. So, you have the following options to handle this.
- Create a C# DLL that contains all your XAML pages as a compiled assembly, and then have your C++ executable include that DLL as a reference.
- Drop XAML and write all your WPF in code, building up the element tree from application.
This article will use the first approach.
There are two basic techniques for interoperation between WPF and Win32/MFC code:
- Host WPF in Win32/MFC: With this technique, developers can use the advanced graphics capabilities of WPF within the framework of standard Win32/MFC applications.
- Host Win32/MFC in WPF: With this technique, developers can use an existing custom Win32/MFC control in the context of other WPF content, and pass data across the boundaries.
This article will be based on the first technique.
Those are all the basic ground rules you should know; it'd time to get to the code. Here, I will show you WPF content created in XAML and C#, and the generated DLL will be referenced in VC++.
The idea is to create a simple Date & Time setting tool with the animated clock part implemented as a WPF content. The rest of the program still will be coded in MFC.
This demo program is constructed by two parts; one is MFCHostWPF (coded in VC++/MFC) and the other is WPFcontrols (coded in XAML and C#). The MFCHostWPF project will take the DLL generated by WPFcontrols project as an external reference.
Adding WPF as a Reference Library in an MFC Project