You are on page 1of 22

Microsoft Foundation Classes

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"

// Define the messages we will respond to and create the window


BEGIN_MESSAGE_MAP (CMainFrame, CFrameWnd) // classname, base class
//ON_WM_CREATE()
//ON_WM_SHOWWINDOW()
//ON_WM_ACTIVATE()
ON_WM_MOVE()
ON_WM_PAINT()
END_MESSAGE_MAP()
Parts of a basic MFC Program – 5b
MainFrame.cpp – part 2
CMainFrame::CMainFrame()// Explicit constructor
{
CString title = "DJ's WIndow";
Create (NULL /* classname*/,
title, WS_OVERLAPPEDWINDOW,
CRect (0, 0, 670, 300),
NULL, /* parent window ptr*/
0, /* ID */
NULL /* create context ptr - only if overriding the
context */
);
}
Parts of a basic MFC Program – 5c
void CMainFrame::OnPaint()
{CPaintDC dc(this);// "this" is a handle for current window
// dc is the "device context" for the window
dc.TextOut(x, y, "hello"); // works for printers or displays
common (this); // call common code and pass the context
}
void CMainFrame::OnMove()
{
}
void CMainFrame::common (CWnd * wptr)
{ // have the window ptr (wptr), but not always using it
}
Window, Dialog & Control classes
• Frame Window
– CFrameWnd, CMenu
• View –
– Represent client area of a frame
– Show data, accept input
• Cview, CScrollView, form & record, control
• Dialog box (modal, modeless)
– Cdialog, CDataExchange
• Control –
– Attached to CFrameWnd or CMiniFrameWnd
– Static, text, number, button, list, toolbar, misc
• Control bar
CFrameWnd
• Framework for a single-document interface
• Usage
– Derive a class using CFrameWnd::Create
– Add member variables for your data
– Implement message handlers
– Define a message map
• Specifies actions when the window gets a msg
Dialog boxes
• Modal
– Requires user interaction
– Blocks program operation until action completed
– Mode errors can be caused by:
• Caps-lock or Insert key
• Focus stealing (user types but nothing happens)
• Modeless
– No interaction required (e.g.; toolbar)

• Note: this happens a lot in Unix/Linux editor, "vi",


because mode indicator can be "off".
Views
• Represent the client area of a frame window
• Associated with document class &
CFrameWnd
• 2 types of document views
– Cview - base class for app-specific views
– CScrollView - base class for scrollable data
• CFormView, CRecordView, CHtmlEditView
• Control views
– CCrtlView, CEditView, CCListView, CTreeView
GetWindowRect
• Win32API version
BOOL WINAPI GetWindowRect ( HWND hWnd,
LPRECT lpRect );
note: TWO parameters, lpRect is a struct

• 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.

• MDI application, the main frame window is


derived from class CMDIFrameWnd, and the
document frame windows, which are MDI
child windows, are derived from class
CMDIChildWnd.

You might also like