Welome to AWT4MFC
AWT4MFC is a UI toolkit for MFC, based on the Java AWT. It is a great tool for building fast, lightweight UIs and creating resusable components that use proper C++ inheritance, with no weird macros or extensions.

This is mature code, it has been in development (on and off) since 1998 and has been successfully deployed in a number of applications. Encouraged by Microsoft's new-found enthusiasm for MFC, I have updated it for VS 2008 and decided to release it to the community.

AWT4MFC is self contained, with no external dependencies or registry settings. It comprises MFC extension DLLs, so is easy to use and integrate with your MFC app, just include the DLLs with your application.

So is anybody out there still writing Windows desktop apps? And if so is it all Windows Forms bloat, or are some people still making small, fast, clever GUI applications? If so, then this might be of interest.

At some point back in the 20th Century, I was working on an application that needed to create complex data entry forms at runtime. Nothing out of the box in MFC seemed to be the right fit, with the CFormView emphasis on design-time form templates. I had done some Java AWT work, thought 'that's what I need', and being young and foolish, I started coding. AWT4MFC is the result of this effort, with various additions and modifications made over the years. "So why not just write your app in Java?" I hear the more managerially-minded of you cry. Well, I never encountered an AWT or Swing application that wasn't ugly and slow. I had been developing in MFC and Win32 for years, and I wanted my application to be fast, slick and small, and there was no way Java would do this for me, what I needed was some equivalent MFC framework.

What's the AWT?
Anybody who's done some Java UI work can skip this bit. AWT stands for Abstract Windowing Toolkit and is the fundamental technology behind Java GUIs, from back in the days when Java was the latest cool thing and was all about building portable desktop and browser based client-side apps. It is a very different way of doing things to the RAD/VB approach, where you lay out objects on a form at design time. In AWT everything derives from a base class Component. Deriving from Component is Container, which contains a list of Component objects. Because Container is also a Component, it can be placed inside another Container. The way components are arranged within a Container depends on the LayoutManager, of which there a several standard ones, and it is easy to roll your own. So the idea is to build up a UI by creating a tree of polymorphic objects, the layout of which is controlled by the layout managers. This can be a nuisance if your requirements are merely simple data entry, but it's great if you need to create something clever, where your UI has to adapt to changes in the size or shape of the screen, and respond in interesting ways to events or other components.

I never set out to do a complete and comprehensive clone of AWT, but rather to create a toolkit that offered equivalent functionality within MFC. Instead of re-inventing core Java classes, AWT4MFC uses familiar MFC classes where there are equivalents. So AWT4MFC uses CString instead of String, CDC instead of Graphics, CPoint instead of Position and so on. Core AWT classes have the prefix AWT, so we have AWTComponent, AWTContainer, AWTLabel etc. corresponding to the core Java Component, Container, Label classes.

Getting Started
  • Download the source code and unzip it somewhere
  • In the root directory of the source tree is AWT4MFC.sln. Open this in VS 2008
  • Build the solution
  • Set TestAWT4MFC as startup project and run it

First AWT4MFC App
1. When you build the AWT4MFC solution, the DLLs are copied to a 'bin' folder under the solution root folder. Add this folder to your path
2. In Visual Studio, create a new MFC Application called FirstAWT4MFCApp. In the Application Wizard, on the 'Application Type' page, choose 'Application Type: Single Document' and 'Project Style: MFC Standard', and uncheck 'Use Unicode Libraries'. Otherwise stick with the defaults.
3. Include “AWT.H” from the AWT4MFC project in stdafx.h and add the path to this file to your project (in project properties->Configuration Properties->C/C++->General->Additional Include Directories)
4. Add AWT4MFC.lib to the project (get this from the output folder of the AWT4MFC project)
5. Open CFirstAWTMFCAppView.h in the editor, and replace all references to CView with AWTView. Do the same in CFirstAWTMFCAppView.cpp
6. Add a new C++ class to the project. In the Generic C++ Class Wizard, set the class name to FirstAWT4MFCApplet, and the base class to AWTApplet. Ignore any warning about AWTApplet not found in the project
7. Open FirstAWT4MFCApplet.h in the editor, and change it as follows:

class FirstAWT4MFCApplet: public AWTApplet
FirstAWT4MFCApplet(CWnd* parent) : AWTApplet(parent){};
virtual ~FirstAWT4MFCApplet(){};

virtual void init();
virtual void start();
virtual void stop();
virtual void destroy();

8. Open FirstAWT4MFCApplet.h in the editor, remove the generated constructor and destructor, and add the following code:
void FirstAWT4MFCApplet::init()
add(new AWTLabel("My first AWT4MFC Application"));

void FirstAWT4MFCApplet::start()

void FirstAWT4MFCApplet::stop()

void FirstAWT4MFCApplet::destroy()

9. Open FirstAWT4MFCApplet.cpp in the editor and make the following changes:
  • add 'FirstAWT4MFCApplet.h' to the list of include files at the top
  • Modify the PreCreateWindow method create an instance of your applet:

BOOL CFirstAWT4MFCAppView::PreCreateWindow(CREATESTRUCT& cs)
// TODO: Modify the Window class or styles here by modifying

m_applet = new FirstAWT4MFCApplet(this);

return AWTView::PreCreateWindow(cs);

10. Compile and run!

What Next?
There's much more to it than this, but hopefully this is enough to get started. If there is any interest in this project, I can post more tutorials and examples.

Last edited Jun 23, 2008 at 7:46 PM by jduminy, version 13