Programming Windows with MFC, Second Edition

Category: Operating Systems
Author: Jeff Prosise
All Stack Overflow 9


by anonymous   2017-08-20

+1 Good question!

tl;dr: Learn Win32 - in that order.

By far the most important prerequisite to MFC is a solid understanding of the Windows API (also sometimes referred to as the Win32 API). You will also need to be familiar with a few key concepts of C++ and get intimate with the tools. My proposed list of prerequisites would be:

  1. Get a solid background in Windows API development.
  2. Familiarize yourself with relevant concepts of C++.
  3. Learn the tools.

The following is a rundown of these steps, with links to additional information.

1. Windows API:

The Windows API exposes all services through a C interface. As a consequence resource management is often tedious boiler plate code. And visible in source code (sometimes an incredible bonus when reading code). MFC is - to a large extent - an automated resource management wrapper (and utility library) around the Windows API, hiding most of the resource management and type conversions. To be fluent in MFC you have to understand what it hides.

To get started with the Windows API and all you need is a quick rundown of the major components I would recommend theForger's Win32 API Tutorial (by forgey of #winprog fame). It covers Message Handling, Windowing, Controls, and the GDI, and builds a solid foundation. If you feel like investing (well spent) time into exploring the Windows API in more detail, the best resource to date is probably still Charles Petzold's Programming Windows (not the be confused with the managed Petzold). The MSDN is also a good resource to get both an overview as well as detailed documentation for specific areas like:

  • Message Handling
  • Windowing
  • User Interaction
  • Windows Controls

2. Key C++ concepts:

MFC is implemented in terms of C++. While it predates the official C++ standard you won't find too much funky business in there. Very little template code and certainly none of the new-fangled C++11 features. Intimacy with basic C++ concepts will get you a long way.

As mentioned previously, MFC is to a large extent a resource management wrapper around Windows API. Often there is a direct mapping between Windows API resources and MFC objects (like HWND : CWnd, or HDC : CDC). If you understand constructors, destructors and object lifetime you're pretty much all set in that department.

When using MFC's template-based containers (like CMap) you will be exposed to templates, naturally. Nothing too involved there, just very basic type parameterization to reuse the container code and enable type-safe element access. An introduction to MFC containers can be found here: Collections.

Exceptions are used rarely in MFC, mostly when accessing files or when serializing data. You should still know how to write exception safe code; you will see exceptions in non-trivial applications. An overview can be found at Exception Handling in MFC.

One area which is not really part of C++ but used extensively throughout the MFC is the preprocessor. Regardless of how trivial your MFC application is, there will be macros. You have to have a very good understanding of both the preprocessor syntax as well as how the preprocessor operates.

3. Tools:

While it is possible to write MFC applications with Notepad alone it is certainly not very efficient to do so. A plain text editor might be a good idea for learning the platform, but when it comes time to meet milestones and deadlines you certainly will want to use a powerful IDE.

MFC pretty much implies the use of Visual Studio and I will assume that's what you're using. If you are using VS6 or VS2010 (or later) you will have access to the MFC Class Wizard. This is a tool you will be using frequently. To safely operate it you must get familiar with the code it generates for you. Play around with it, inspect the generated code, and try to build a mental model of it. If it doesn't make sense yet you should return later.

The MFC class wizard will almost certainly generate preprocessor code. It hides a tremendous amount of complexity which you need to understand to use safely. Use the IDE to navigate through the preprocessor macros, try to expand them in your mind, and see if you understand the resulting code. If you are having difficulty unmangling the macro invocations have the compiler output the preprocessed code for you using the /P (Preprocess to a File) compiler option.

Occasionally you have to look or debug into the MFC source code. To make the source code available to source browsing and the debugger you have to set up the VC++ Directories to include the following (if not already present):


Depending on the IDE this is either done through Tools -> Options: Projects and Solutions -> VC++ Directories or the property sheet of your project settings.

Finding help: While the MSDN is the best resource for documentation, the MFC section feels like it is in maintenance mode and doesn't get as much attention as the Windows API documentation. If you find the MFC documentation lacking look up the respective Windows API documentation instead, for example CWnd::OnNcDestroy vs. WM_NCDESTROY. The latter contains valuable information on the order in which windows receive this message.

Learning MFC

A comprehensive resource for learning MFC is Jeff Prosise' Programming Windows with MFC. While it is dated (released in 1999) the concepts are still valid today. It provides an overview of the concepts of MFC and goes deep into the implementation details. If you find yourself struggling with the generated (preprocessor) code this book is for you.

As a valuable online resource the MSDN offers information on just about any aspect of MFC development. The major concepts include:

  • Message Handling and Mapping
  • User Interface Elements
    • Windows
    • Dialog Boxes
  • Serialization in MFC

A full list can be found at Concepts.

by anonymous   2017-08-20

Petzold for learning the raw windows API

This will give you a good foundation for groking how things work under the hood. This is important because MFC is essentially a wrapper and and sometimes you have to get lower level. You'll be a much stronger developer if you don't depend on the IDE and wizards for everything. Petzold strongly emphasizes this.

Prosise is good for MFC:

by anonymous   2017-08-20

Given the comment of "say Windows XP as an example", then your options are:

  • Interact directly with the operating system via its API, which for Microsoft Windows is surprise surprise call Windows API. The definitive reference for the WinAPI is Microsoft's MSDN website. A popular online beginner tutorial for that is theForger's Win32 API Programming Tutorial. The classic book for that is Charles Petzold's Programming Windows, 5th Edition.

  • Use a platform (both in terms of OS and compiler) specific library such as MFC, which wraps the WinAPI into C++ class. The reference for that is again MSDN. A classic book for that is Jeff Prosise's Programming Windows with MFC, 2nd Edition. If you are using say CodeGear C++ Builder, then the option here is VCL.

  • Use a cross platform library such as GTK+ (C++ wrapper: gtkmm), Qt, wxWidgets, or FLTK that wrap the specific OS's API. The advantages with these are that in general, your program could been compiled for different OS without having to change the source codes. As have already been mentioned, they each have its own strengths and weaknesses. One consideration when selecting which one to use is its license. For the examples given, GTK+ & gtkmm is license under LGPL, Qt is under various licenses including proprietary option, wxWidgets is under its own wxWindows Licence (with a rename to wxWidgets Licence), and FLTK is under LGPL with exception. For reference, tutorial, and or books, refer to each one's website for details.