Lect2 - 2009 1 API MFC
Lect2 - 2009 1 API MFC
Lect2 - 2009 1 API MFC
MFC Programming
? MFC:
MFC
? The
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 app can use either Win32 API, MFC, or both
Produce smaller executables ? Can lead to faster program development ? MFC Programs must be written in C++ and require the use of classes
?
On the Web:
http://msdn.microsoft.com/en-us/library/d06h2x6e(VS.80).aspx
Serialization Runtime class information Diagnostic & Debugging support Some important macros
? CCmdTarget:
? CCmdTarget
CDocument
directly
Use global scope resolution operator
Example ::UpdateWindow(hWnd);
members of any MFC class ? Independent of or span MFC class hierarchy ? Example:
AfxMessageBox()
API mechanism: switch/case statement in apps WndProc Under MFC, WndProc is buried in MFC framework Message handling mechanism: Message Maps "
lookup tables the MFC WndProc searches
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
need to have different views of same data ? Doc/View approach achieves this separation:
Encapsulates data in a CDocument class object Encapsulates data display mechanism & user interaction in a CView class object
Document/View Programs
Almost always have at least four classes derived from: CFrameWnd CDocument CView CWinApp ? Usually put into separate declaration (.h) and implementation (.cpp) files
?
? Lots
?
of initialization code
AppWizard
Tool that generates a Doc/View MFC program framework automatically ? Can be built on and customized by programmer ? Fast, efficient way of producing Windows Apps ? Creates functional CFrameWnd, CView, CDocument, CWinApp classes ? After AppWizard does it's thing:
?
Application can be built and run Full-fledged window with all common menu items, tools, etc.
Connect resources & user-generated events to program response code Insert code into appropriate places in program
Code then can then be customized by hand
Create new classes or derive classes from MFC base classes Add new member variables/functions to classes
?
SKETCH Application
? Example
of Using AppWizard and ClassWizard ? User can use mouse as a drawing pencil Left mouse button down:
lines in window follow mouse motion
? Left
sketching stops
? User
Sketch data (points) won't be saved So leave document (CSketchDoc) class created by AppWizard alone ? Base functionality of application (CSketchApp) and frame window (CMainFrame) classes are adequate Leave them alone ? Use ClassWizard to add sketching to CSketchView class
?
Sketching Requirements
? Each
Variables
? BOOLEAN
3. Build Application --> Full-fledged SDI App with empty window and no functionality
Scroll to WM_LBUTTONDOWN & select it Add the handler by clicking on down arrow and <Add> OnLButtonDown
Note that the function is added in the edit window and the cursor is positioned over it: After TODO enter following code: m_butdn = TRUE; m_ptold = point;
? Repeat
There are many other SetWindowXxxxx() functions that can be used to change other properties of the window
We write the OnXxx() handler function Must be declared in .h file and defined in .cpp file
?
Click on Add and edit After TODO in editor enter following code: m_color = RGB(255,0,0);
Repeat for ID_DRAWINGCOLOR_BLUE Code: m_color = RGB(0,0,255); Repeat for ID_DRAWINGCOLOR_GREEN Code: m_color = RGB(0,255,0); Repeat for ID_DRAWINGCOLOR_BLACK Code: m_color = RGB(0,0,0); Repeat for ID_CLEAR Code: Invalidate();