Professional Documents
Culture Documents
What is MFC?
• Set of C++ classes written by MS
• Simplifies writing complex programs
• Covers many areas:
– GUI
– I/O
– O/S interfaces
– ActiveX
• Framework for IE extensions
• Component of Windows
Why Use MFC?
• Faster development
• More code is reusable
• Many common tasks are "built-in"
– Winmain, WndProc
• Smaller executable
• Uses all the C++ paradigms
– Inheritance, polymorphism, etc.
MFC class categories
• CObject – the root class
• Application architecture classes
• Window, dialog, control
Used in
• Drawing, printing
this course
• Datatypes
• Array, list, map
• File & DB
• Internet & networking
• OLE
• Debugging
Cobject
• Think of it as the "master" class
• Serialization
– Stream data to/from devices (disks)
• Runtime class info
• Debugging
• Some derived classes
– Cmenu (menus & menu mgmt.)
– CDC (device context & drawing)
– CGdiObject (drawing devices – brushes, etc)
Related info
• MFC class hierarchy chart
• Macros & global variables
• WndProc (Win32API) buried in MFC library
• Message Maps
– Names of events
– Handler functions for those events
– E.g.; ON_WM_MOVE for CMainFrame::OnMove
– ON_WM_MOVE pre-defined constant
– OnMove pre-defined fn in the CMainFrame class
WIn32API vs MFC Message handling
Window Window
gets gets
moved moved
WndProc() CMainFrame::OnMove
case WM_MOVE:
movefn();
Win32API MFC
Parts of a basic MFC Program - 1
MyWinApp.h
#include <afxwin.h>
class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
Parts of a basic MFC Program – 2
MainFrame.h
#include <afxwin.h>
class CMainFrame : public CFrameWnd
{ private: // variables known to all CMainFrame members
public: // define the prototypes for functions to handle window events
CMainFrame(); // class constructor
// prototypes for members named in the MESSAGE MAP
// The MESSAGE MAP is implemented in MainFrame.cpp
//afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
afx_msg void OnMove(int x, int y);
afx_msg void OnPaint(); // msg hndlr for WM_PAINT event
void common(CWnd* wptr);
DECLARE_MESSAGE_MAP();
}; // note: the "afx_msg" prefix is pre-VS 2005 and is #defined as empty
Parts of a basic MFC Program – 3
// Main.cpp
#include "MyWinApp.h"
CMyWinApp MyApplication;
Parts of a basic MFC Program – 4
// MyWinApp.cpp
// create the window frame & display it
#include "MyWinApp.h"
#include "MainFrame.h"
BOOL CMyWinApp::InitInstance()
{ CMainFrame *pFrame;
pFrame = new CMainFrame;
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
Parts of a basic MFC Program – 5a
MainFrame.cpp
• This is where all the real work gets done
#include "MainFrame.h"
• MFC version:
void GetWindowRect ( LPRECT lpRect ) const;
note: one parameter, lpRect is a class (a Crect object)
Document/View Architecture
• Display of data should be independent of the
data
• May need to have different ways to look at the
data
• CDocument class holds the data
• CView manages display and UI
• 2 kinds of apps:
– Single Document Interface
– Multiple Document Interface
View
• Display of the data & a way to display it
• Frame window: contains a view of the data
• Multiple views possible
• But only ONE doc per View
tips
• SDI application
– One frame window derived from class
CFrameWnd.
• This window is both:
– main frame window
– and document frame window.