Introduction to Microsoft
Foundation Classes
Background on Object Oriented
Programming
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Modularity
GUID, FPGDST 2004-2005, C-DAC
Design Goals of MFC
• Deliver a real-world application
– Using C++ and Object Oriented techniques
• Deliver an small and fast application framework
– To simplify Windows development
– Allow developers to leverage their existing knowledge
of Windows
– To build large-scale applications that utilize the latest
Windows enhancements, such as OLE and ODBC
GUID, FPGDST 2004-2005, C-DAC
Delivering a real-world application
• No new programming concepts
– Extends the object-oriented programming model that
was already used by Windows
– Expresses their concepts in standard C++ idioms
• Subset of C++ is defined in MFC
– However, no restrictions were placed on code that a
programmer could write
– you can use any feature of C++ you want to
GUID, FPGDST 2004-2005, C-DAC
Simplifying the Windows API..
• API functions are grouped into logical units.
– All functions related to device context appear in one
class and all functions related to a generic window
appear in one…
• Safely hides away the messy details of Windows
programming
– such as registering windows classes, window and
dialog procedures, and message routing
GUID, FPGDST 2004-2005, C-DAC
..Simplifying the Windows API
• Strong typing and data-encapsulation features of
C++ is used
– To make development mechanically easier
– e.g. Every windows message uses two parameters,
WPARAM and LPARAM. However the actual values
stored in the message maybe any number of things:
• pointers, POINT structures, windows handles, etc.
– To retrieve the parameters, the C/SDK programmer
needs to decode the parameters into proper type of
values
GUID, FPGDST 2004-2005, C-DAC
Using the knowledge you already
have
• The class hierarchy and naming conventions on
the underlying API naming conventions
– To minimize the relearning curve for experienced
developers
• So converting a standard program written using
SDK to MFC is fairly simple
– Even conversion might not be required
GUID, FPGDST 2004-2005, C-DAC
A Firm Foundation
• The Windows API keeps getting bigger and bigger
– As it continues to collect new material
– Microsoft keeps adding new APIs to Windows
including OLE and ODBC
• Initial framework does not support every new
Windows API
– Was designed so that it could be readily extended.
Every new API introduces a new learning curve
– MFC eases things by encapsulating the details of an
API in a set of classes
GUID, FPGDST 2004-2005, C-DAC
Application Framework..
• Collection of classes which work in a well defined
manner
• Defines fixed steps to create an application
• Developer does not have much control
• Provides hooks for plugging in developer code
GUID, FPGDST 2004-2005, C-DAC
..Application Framework
GUID, FPGDST 2004-2005, C-DAC
A Tour of MFC
• General-Purpose classes
– CObject, CException
• Windows API classes
– CCmdTarget, CWinThread, CWinApp
• Application framework classes
– CDocument, CView
• High Level abstractions
– ScrollView, Splitter
GUID, FPGDST 2004-2005, C-DAC
Important Classes
• CWinApp
• CWnd
• CFrameWnd
• CView
• CDocument
GUID, FPGDST 2004-2005, C-DAC
CWinApp
• Represents the application itself
• User application derived from it
• User action by overriding methods
GUID, FPGDST 2004-2005, C-DAC
CWnd
• Represents a window
• Used for display purposes
• Also receives events
GUID, FPGDST 2004-2005, C-DAC
CFrameWnd
• Derived from CWnd
• Contains menus, toolbars, status bars and
view window
• Constitutes main window of application
GUID, FPGDST 2004-2005, C-DAC
CView
• Represents view window
• Used in Document/View
• Created as a child of CFrameWnd
• Used to display contents of document
• Receives events
GUID, FPGDST 2004-2005, C-DAC
CDocument
• Represents document object
• Used in Document/View
• Created as an independent object
• Tightly bound to view object
GUID, FPGDST 2004-2005, C-DAC
Application Without Doc/View
• Minimum application
• Event handling
• Window painting
• Menus
• Dialog boxes
GUID, FPGDST 2004-2005, C-DAC
Basic Windows Support
• Windows sits between the hardware and the
applications it is hosting
– Whenever anything happens that is of interest to one of
the applications, Windows informs the application
• A substantial amount of code is required by a
windows application to receive and process events
GUID, FPGDST 2004-2005, C-DAC
The tasks to be accomplished
• The Windows application needs to set up a message
handler and plug it into Windows (via RegisterClass()
API)
– so that Windows knows where to go and get messages
handled for a specific application
• Windows needs to keep track of specific instances of an
application
• The application asks Windows for the next event in the
message queue, dispatches the message to the
appropriate message handler, and then gets the next one
• This activity continues till the application ends
GUID, FPGDST 2004-2005, C-DAC
Requirements of a Windows
Program..
• The program must contain a WinMain() function.
– Just as every C program requires a main() function, every
– WinMain() acts as the entry point for the program.
• It is through the WinMain() function that the application receives
from Windows the information necessary for it to run
• The application must register at least one window
class to serve as the main window
– It is the window’s job to present the user interface on the
screen
GUID, FPGDST 2004-2005, C-DAC
..Requirements of a Windows
Program
• Most applications require some initialization and setup
• This initialization usually takes two forms
– application-specific
– instance-specific initialization
– The program needs to provide any necessary initialization
steps
• The application must provide a message handler
– The function of the window procedure is to handle messages
– At the very least, the window procedure must handle the
WM_DESTROY message
GUID, FPGDST 2004-2005, C-DAC
Sample - Minimum Application
GUID, FPGDST 2004-2005, C-DAC
Minimum Application
• Create application object
– derive application object from CWinApp
– override InitInstance() method
– in InitInstance(), create application main window
instance and display it
– store window instance in m_pMainWindow member
• Create application main window object
– derive CFrameWnd
– in constructor create window using Create
GUID, FPGDST 2004-2005, C-DAC
A minimum Application
#include <afxwin.h>
//Define an application class derived from CWinApp
class CGenericApp : public CWinApp {
public:
virtual BOOL InitInstance();
};
class CGenericWindow : public CFrameWnd {
public:
CGenericWindow() {
Create(NULL, “Generic”);
}
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
GUID, FPGDST 2004-2005, C-DAC
A minimum Application
BEGIN_MESSAGE_MAP(CGenericWindow, CFrameWnd)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
Void CGenericWindow::OnLButtonDown(UINT nFlags, Cpoint point) {
MessageBox( “Left mouse button pressed…”, NULL, MB_OK);
}
BOOL CGenericApp::InitInstance() {
m_pMainWnd = new CGenericWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CGenericApp GenericApp;
GUID, FPGDST 2004-2005, C-DAC
Analysis..
• This program looks different from a C/SDK program
– The WinMain() function is gone, and
– There is no code that seems to be registering the window
classes
• There is no message handling function anywhere
– It seems the message loop hasn’t been created
• Most of the implementation details have been hidden
from the programmer
– The framework does most of the job for the programmer and
have been hidden inside macros like
BEGIN_MESSAGE_MAP
GUID, FPGDST 2004-2005, C-DAC
..Analysis
• WinMain() is part of the application framework and
MFC’s
– WinMain() does a very good job initializing the variables,
creating the window and setting up the message loop
• The function InitInstance() is called by the WinMain()
– Handles the creation of the window and registers the
window classes too
• MFC’s message handling still uses the same functions
GetMessage(), TranslateMessage() and
DispatchMessage()
– But at the lowest level because these functions have been
encapsulated
GUID, FPGDST 2004-2005, C-DAC
The IDE
• To start a new MFC project…
GUID, FPGDST 2004-2005, C-DAC
The IDE
• There are many options in the following dialogs, but for
the time being, choose
– Choose “Dialog Based” & Check “Windows Sockets”
GUID, FPGDST 2004-2005, C-DAC
The IDE
• Now you have
– C++ files & different
classes for your project
– Basic GUI Resources
file
GUID, FPGDST 2004-2005, C-DAC
The IDE
• The Resource Editor
Visual Studio IDE provides us with
WYSIWYG editor to generate the
definition file for us.
GUID, FPGDST 2004-2005, C-DAC
The IDE
• Know the class
structure
The application class
The Dialog’s (GUI) class
Start your code here
GUID, FPGDST 2004-2005, C-DAC
The IDE
• Adding components in the generated dialog
To view/add/edit
messages,
handler
functions, Select UI
member controls
variables by here
ClassWizard
GUID, FPGDST 2004-2005, C-DAC
The IDE
• View/Add Event Handlers
GUID, FPGDST 2004-2005, C-DAC
The IDE
• Adding event handler functions
– After double clicking on the button, the IDE helps you to
register a listener function that handles BN_CLICKED
message
Add your
codes for
handling click
event here
GUID, FPGDST 2004-2005, C-DAC
References
• MSDN Online
• Beginning Visual C++ 6
– Ivor Horton, Wrox Publication
• Learn Microsoft Visual C++ 6.0 Now
– Chuck Sphar, Microsoft Press
• Samples
– min
– appwiz
GUID, FPGDST 2004-2005, C-DAC