Microsoft Foundation Classes
Microsoft Foundation Classes
History
MFC was introduced in 1992 with Microsoft's C/C++ 7.0 compiler for use with 16-bit
versions of Windows as an extremely thin object-oriented C++ wrapper for the Windows
API. C++ was just beginning to replace C for development of commercial application
software at the time.
One interesting quirk of MFC is the use of "Afx" as the prefix for many functions,
macros and the standard precompiled header name "stdafx.h". During early development
what became MFC was called "Application Framework Extensions" and abbreviated
"Afx". The name Microsoft Foundation Classes (MFC) was adopted too late in the
release cycle to change these references.
MFC 8.0 was released with Visual Studio 2005. MFC 9.0 was released with Visual
Studio 2008. MFC is not included in the free edition of Visual C++ 2005/2008/2010
Express.[3]
The Object Windows Library (OWL), designed for use with Borland's Turbo C++
compiler, was a competing product introduced by Borland around the same time.
Eventually, Borland discontinued OWL development and licensed the distribution of the
MFC headers, libraries and DLLs from Microsoft[4] for a short time, though it never
offered fully integrated support for MFC. Borland later released VCL (Visual Component
Library) to replace the OWL framework.
Microsoft's emphasis on MFC has been reduced in favor of its .NET Framework. MFC 7,
8 and 9 bridge elements of MFC with the .NET Framework to aid developers in
migrating to the new framework. The MSVC++ compiler backend can emit managed and
native object file(s). The linker can then build them together, generating hybrid (both
managed and native) applications, allowing existing native applications to use managed
extensions in a seamless manner. Though Microsoft has de-emphasized MFC, it remains
a popular and widely used framework.
A lightweight alternative to MFC is the Windows Template Library (WTL). C++ Express
version compiles WTL applications, but does not include the IDE support of the
Standard, Professional and Team editions. In an MFC program, direct Windows API calls
are rarely needed. Instead, programs create objects from MFC classes and call member
functions belonging to those objects. Many of those functions share their names with
corresponding API functions.[5]
[edit] Features
When MFC was introduced, it provided C++ macros for Windows message-handling (via
Message Maps), exceptions, run-time type identification (RTTI), serialization and
dynamic class instantiation. The macros for message-handling were intended to reduce
memory consumption by avoiding gratuitous virtual table use and also provide a more
concrete structure for various Visual C++-supplied tools to edit and manipulate code
without parsing the full language. The message-handling macros replaced the virtual
function mechanism provided by C++.
The macros for serialization, exceptions, and RTTI predated availability of these features
in Microsoft C++ by a number of years. 32-bit versions of MFC, for Windows NT 3.1
and later Windows operating systems, used compilers that implemented the language
features and updated the macros to simply wrap the language features instead of
providing customized implementations, realizing upward compatibility.
Windows Programming/Introduction to
MFC
o
Some tools, such as Microsoft Visual Studio, are capable of automatically generating
large amounts of MFC skeleton code for use in a project. Because of this, most MFC
tutorials or reference materials will teach the subject using the automated Visual Studio
tools, and leave out some of the gritty details. In this book where possible we try to be
neutral.
MFC was first oriented mostly for enterprise-level programming projects, created in an
age most code was done in C and Object Oriented Programming was only in the realm of
Smalltalk.
Since the release of Visual Studio 6.0 and the MFC 6.0 little was known of the future
support to the MFC since the company was favoring the .NET Framework. Version 7.0,
7.1 and 8.0 were mostly extensions to support the new OSs and to aid developers in
migrating to the new framework. Since then information on the future of the MFC could
be only extracted from Steve Teixeira, Microsoft Corporation, June 2005 paper - MFC:
Visual Studio 2005 and Beyond (http://msdn2.microsoft.com/en-
us/visualc/aa442855.aspx), on the release of Visual Studio 2008 Service Pack 1,
Microsoft seems to once again be actively supporting the MFC.
Today the common users find acceptable for a low complexity program having a memory
footprint of 30-80Mb (common in Java or .Net applications), low response times or
"outside of you control" applications like the WEB now provides, so it is debatable if the
impact of required for the use of MFC in small applications outbalanced any of the
benefits the libraries provides, most of the software made specifically for Windows today
uses MFC.
You should prefer the Win32 API SDK, or an alternative wrapper for it, if you do not
intend to:
The MFC design principle is an attempt for simplification. The wrapper classes were
designed so to simplify some tasks and automates others. Because of those facts,
however, a certain amount of fine-tunable control was lost from the raw Win32 API or
excessive automation was archived. The MFC has been recognized as having serious
design flaws and inconsistencies and has not been actively maintained. The C++
language and best practices have evolved and today this constitutes a barrier for the
utilization of the framework.
As MFC predates the STL some of its implementations are not the best, you should also
try to use the STL when ever you can, it is part of the C++ standard now and multi-
platform so you will save some time if you later decide to port the code.
STL containers
The MFC implements its own versions of the STL containers, even if inconsistently
implemented, if future portability of the code is not a consideration the MFC
implementations are quicker.
Multiple Inheritance
The MFC class library does not use and Multiple Inheritance and was not designed with
full support for Multiple Inheritance.
Since most MFC classes derive from CObject using Multiple Inheritance will be cause
problems of ambiguity (any reference to CObject member functions will have to be
disambiguated). Static member functions, including operator new and operator
delete must also be disambiguated.
The best option is to avoid the use of Multiple Inheritance with MFC but take a look at
Using C++ Multiple Inheritance with MFC (msdn.microsoft.com) for the needed
information on how to bypass the limitations.
The MFC uses the Hungarian notation. It uses prefixes, like "m_" to indicate a member
variable or "p" to indicate a pointer, and the rest of the name is normally written out in
PascalCasing (the first letter of each word is capitalized).
All the significant classes in MFC derive from the CObject class. The CObject does not
have any member data, but does have some default functionality.
[edit] Using MFC
MFC requires header files that are separate from the standard <windows.h> header file.
The core of the MFC system requires the inclusion of <afxwin.h>. Other header files that
are of some use are <afxext.h> (for MFC extensions), and <afxcmn.h> (for the MFC
common dialog boxes).
Simply changing the header files, unfortunately, is still not enough. The MFC DLL
libraries must be linked to by the project, because the DLL files contain the class
definitions that will be used throughout every program. To use MFC, you must link to the
MFC libraries
[edit] stdafx.h
stdafx.h is the standard include for MFC projects - that is, if you create a new MFC
project, a stdafx.h will automatically be created for you. It will include all the rest of the
necessary MFC header files.
[edit] theApp
Use in the header file of your application class and then include it wherever you need to
use theApp.
You could try the AfxGetApp function to get a pointer to theApp, an efficient method of
accessing members of your application is to make a pointer to theApp a member variable
of the class which needs it -- for example:
// Attributes
public:
CMdiApp* m_pApp;
};
and make sure you initialize m_pApp in the constructor or else will be accessing a NULL
pointer.
CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/):
CDialog(CMyDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDialog)
//}} AFX_DATA_INIT
// Outside the special-format comments
above...
m_pApp = (CMdiApp*)AfxGetApp( );
}
and voila! Now any time you need access to your application, you got it!
m_pApp->m_nMemberVar;
m_pApp->MemberFunction(nParam1, strParam2);
The root class for MFC is the CObject class. CObject itself does not support multiple
inheritance, but derivative classes do. Each application begins in a class derived from
CWinApp. Every program must have a CWinApp class, and each application may only
have one. CWinApp contains a number of functions for initializing an application, and
controlling the instance handle (similar to the HINSTANCE member of the WinMain
function). Programs that want to display a window must utilize a derivative of the CWnd
class.
CWaitCursor aWaitCursor;
[edit] Threads
As it should be expected the MFC also wraps the Win32 thread primitives in special
classes and functions of its own. For generic information of threads and C++ see the C++
Programming Wikibook section on Multitasking. The MFC devised threads in worker
threads and GUI threads.
Worker threads are especially useful to perform background tasks, or any asynchronous
work that doesn't require user intervention, a print job, calculations, waiting for an event,
etc. To create a worker thread, the simplest way is to implement a function that will
perform the desisted work, and then create the thread with AfxBeginThread() that will
use that specific function.
[edit] Communication
TODO
Complete
A proper thread exit can be archived by a normal return from the thread (completion) or
by signaling it to return prematurely. Since we are working on Windows and on a 32 bit
bound CPUs (making 32 bits accesses atomic), it is considered safe (but not portable) to
use a shared bool variable to indicate to a thread to exit, any other synchronization
methods can be used if available.
Since the only issue regarding thread termination is on aborting a job or exiting the
program without having threads running, when dealing with worker threads, in the class
that created the thread on the destructor you should signal the thread to abort and then use
WaitForSingleObject() to wait for the thread to terminate.
Retrieved from
"http://en.wikibooks.org/wiki/Windows_Programming/Introduction_to_MFC"
Categories: TODO | Windows Programming
Hidden category: TODO/Windows Programming
What do you think of this page?
Please take a moment to rate this page below. Your feedback is valuable and helps us
improve our website.
Personal tools
• New features
• Log in / create account
Namespaces
• Module
• Discussion
Variants
Views
• Read
• Edit
• View history
Actions
Search
Navigation
• Main Page
• Help
• Browse
• Cookbook
• Wikijunior
• Featured books
• Recent changes
• Donations
• Random book
Community
• Reading room
• Community portal
• Bulletin Board
• Help out!
• Policies and guidelines
• Contact us
Print/export
• Create a book
• Download as PDF
• Printable version
Toolbox
• Privacy policy
• About Wikibooks
• Disclaimers