Introduction To Microsoft Windows MFC Programming The Application - Window Approach
Introduction To Microsoft Windows MFC Programming The Application - Window Approach
Microsoft Foundation Class (MFC) Library ? A Hierarchy of C++ classes designed to facilitate Windows programming ? An alternative to using Win32 API functions ? A Visual C++ Windows application can use either Win32 API, MFC, or both
e.g., WinMain, the Window Procedure, and the message loop are buried in the MFC Framework
? ? ?
5. MFC Programs must be written in C++ and require the use of classes
Instantiation, encapsulation, inheritance, polymorphism
? ?
Clicking on a class ? a document with a link to the class members Also look at
MFC | hierarchy hierarchy chart
Serialization
Streaming an objects persistent data to or from a storage medium (disk reading/writing)
Runtime class information Diagnostic & Debugging support Some important macros
?
Support for file operations ? CDC: Encapsulates the device context (Graphical Drawing) ? CGdiObject: Base class for various drawing objects (CBrush, CPen, CFont, etc.) ? CMenu: Encapsulates menus and menu management
? CCmdTarget
CDocument
Encapsulates the data associated with a program
ShowWindow( ) -- a member of CWnd class TextOut( ) -- a member of CDC class LoadBitmap( ) -- a member of CBitmap class
? Applications
directly
Use Global Scope Resolution Operator (::), for example: ::UpdateWindow(hWnd);
? Usually
members of any MFC class ? Begin with Afx prefix ( A pplication F rameworKS) ? Independent of or span MFC class hierarchy ? Example:
AfxMessageBox( )
Message boxes are predefined windows Can be activated independently from the rest of an application Good for debugging
Simplest MFC programs must contain two classes derived from the hierarchy: 1. An application class derived from CWinApp
Defines the application provides the message loop
To use these & other MFC classes you must have: #include <Afxwin.h> in the .cpp file
Table entries:
Message number Pointer to a message-processing function
These functions are members of CWnd We override/extend the ones we want our program to respond to Like virtual functions
Message Mapping
? Programs
must:
DECLARATION (.h)
1. Declare a class derived from CWnd or CFrameWnd (e.g., CMainWin)-? Class Members:
The constructor declaration Message-processing function declarations for messages the application will override and respond to
e.g., void OnChar( )
DECLARE_MESSAGE_MAP( ) macro:
Allows windows based on this class to respond to messages Declares that a message map will be used to map messages to handler functions defined in the application Should be last class member declared
Purpose is for application to initialize itself Good place to put code that does stuff that has to be done each time program starts
IMPLEMENTATION (.CPP)
1. Define constructor for class derived from CFrameWnd (e.g., our CMainWin)
?
2. Define message map for class derived from CFrameWnd (e.g., our CMainWin)-BEGIN_MESSAGE_MAP(owner class, base class) // List of message-mapping macros, e.g. ON_WM_CHAR( ) END_MESSAGE_MAP( )
3. Define (implement) message-processing functions declared in .h file declarations above 4. Define (implement) InitInstance() overriding function-?
(Used to refer to the window, like hWnd in API programs) Invoke object's ShowWindow( ) member function Invoke object's UpdateWindow( ) member function Must return non-zero to indicate success
? Now
nature & form of simple window & application have been defined ? But neither exists-? Must instantiate an application object derived from CWinApp our CApp
5. Instantiate the app class ( e.g., our CApp) ? Causes AfxWinMain( ) to execute
It's now part of MFC [WINMAIN.CPP]
? AfxWinMain(
2. Calls CApp::InitInstance( ) [virtual function overridden in 4 above]- which creates, shows, and updates the window
3. Calls CWinApp::Run( ) [In THRDCORE.CPP]- which calls CWinThread::PumpMessage( )- which contains the GetMessage( ) loop
? After
CWinApp::Run( ) returns:
(i.e., when the WM_QUIT message is received) AfxWinTerm ( ) is called- which cleans up and exits
key pressed ?
To respond to messages:
WM_CHAR WM_LBUTTONDOWN WM_RBUTTONDOWN
pDC = this->GetDC( );
Use GetDC( ) member function of this CWnd to get a device context to draw on