MFC Windows Programming (App/Window Approach)
MFC Windows Programming (App/Window Approach)
(App/Window Approach)
Introduction to Microsoft
? The Microsoft Foundation Class (MFC)
Windows MFC Programming: Library
the Application/Window ? A Hierarchy of C++ classes designed
1
MFC Characteristics, continued MFC Class Hierarchy
? 4. MFC Programs must be written in C++ ? (See online help on "Hierarchy Chart")--
and require the use of classes
? Programmer must have good grasp of:
– How classes are declared, instantiated, and used
– Encapsulation
– Inheritance
– Polymorphism--virtual functions
? All its functionality is inherited by any objects (brushes, pens, fonts, etc.)
classes derived from it ? CMenu: Encapsulates menu management
2
MFC Classes and Functions ? Apps can also call API functions directly
? Primary task in writing MFC program--to create – Use Global Scope Resolution Operator, for
classes example:
? Most will be derived from MFC library classes – ::UpdateWindow(hWnd );
? MFC Class Member Functions-- ? Usually more convenient to use MFC
– Most functions called by an application will be member functions
members of an MFC class
? Examples:
– ShowWindow()--a member of CWnd class
– TextOut ()--a member of CDC
– LoadBitmap()--a member of CBitmap
#include <Afxwin.h>
3
Message Mapping
? Programs must:
– Declare message-processing functions
• e.g., OnWhatever() for WM_WHATEVER message
– Map them to messages app is going to respond to
• Mapping done by "message -mapping macros”
• Bind a message to a handler function
• e.g., ON_WM_WHATEVER()
? Most MFC application windows use a window procedure,
WndProc(), supplied by the library
? Message maps enable library window procedure to find the
function corresponding to the current msg.
4
3. Define (implement) message-processing
? Now nature & form of simple window &
functions declared in declarations (1) above
application have been defined
4. Define (implement) InitInstance() overriding
? But neither exists --
function--
? Must instantiate an application object
? Done in class derived from CWinApp (CApp):
– Should have initialization code for each new app instance:
derived fromCWinApp (CApp)
• Create a CMainWin object ? pointer to program's main window
– (Used to refer to the window, like hWnd in API programs)
• Invoke object's ShowWindow() member function
• Invoke object'sUpdateWindow() member function
• Must return non-zero to indicate success
– [MFC's implementation of WinMain() calls this function]
5
How It Works MSG1 Example MFC
CApp object is created ?
MFC's WinMain() executes ?
Application: Mouse/Character
Registers class (default) Message Processing
Calls our CApp::InitInstance() ?
Our override creates a CMainWin object ? User presses mouse button?
Our CMainWin constructor calls Create()? window created – Left/Right Button down string displayed at
Our CApp::InitInstance() override calls window's current mouse cursor position
ShowWindow()? window is displayed
Our override calls UpdateWindow()? client area painted
? Keyboard key pressed?
WinMain() continues by calling its Run() function? – Character displayed at upper left hand corner of
Call to PumpMessage() client area
Which starts the message loop
MSG1
? Global integers to keep track of where text
is to appear
? Global string to hold text to be displayed
? Getting a DC:
– CPaintDC dc(this)
• Constructor performs CWnd::BeginPaint ()
• Destructor performs CWnd::EndPaint ()
• ‘this’: points to the object from which the
member function is called
• Here it’s a pointer to this window
• So we construct a DC for this window