0% found this document useful (0 votes)
96 views

MFC Windows Programming (App/Window Approach)

The document provides an overview of Microsoft Windows MFC programming using the application/window approach. Some key points: 1. MFC is a C++ class library that provides a framework for building Windows applications and encapsulates much of the Win32 API functionality. 2. MFC programs typically derive two main classes - an application class from CWinApp and a window class usually from CFrameWnd. These define the application and main window respectively. 3. MFC uses message maps and member message handling functions to process Windows messages, providing an alternative to large switch statements in raw Win32 code. Derived classes override relevant message handlers. 4. Important MFC classes include CWinApp, CWnd

Uploaded by

Ana Zaefty
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

MFC Windows Programming (App/Window Approach)

The document provides an overview of Microsoft Windows MFC programming using the application/window approach. Some key points: 1. MFC is a C++ class library that provides a framework for building Windows applications and encapsulates much of the Win32 API functionality. 2. MFC programs typically derive two main classes - an application class from CWinApp and a window class usually from CFrameWnd. These define the application and main window respectively. 3. MFC uses message maps and member message handling functions to process Windows messages, providing an alternative to large switch statements in raw Win32 code. Derived classes override relevant message handlers. 4. Important MFC classes include CWinApp, CWnd

Uploaded by

Ana Zaefty
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MFC Windows Programming

(App/Window Approach)
Introduction to Microsoft
? The Microsoft Foundation Class (MFC)
Windows MFC Programming: Library
the Application/Window ? A Hierarchy of C++ classes designed

Approach to facilitate Windows programming


? An alternative to using Win32
API functions
? A Visual C++ Windows application can use
either Win32 API, MFC, or both

Microsoft Foundation Classes


? About 200 MFC classes (versus 2000+ API
functions)
? Provide a framework upon which to build
Windows applications
? Encapsulate most of the Win32 API in a set
of logically organized classes

Some characteristics of MFC: MFC Characteristics, continued


? 1. Convenience of reusable code:
– Many tasks common to all Windows apps are
? 2. Produce smaller executables:
provided by MFC – Typically 1/3 the size of their API counterparts
– Our programs can inherit and modify this ? 3. Can lead to faster program development:
functionality as needed – But there's a steep learning curve--
– We don't need to recreate these tasks – Especially for newcomers to object -oriented
– MFC handles many clerical details in Windows programming
programs

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

Some Important MFC Classes Important Derived Classes--


? CObject: At top of hierarchy ("Mother of all ? CFile: Support for file operations
classes")
? CArchive: Works with CFile to facilitate
? Provides features like: serialization and file I/O
– Serialization
? CDC: Encapsulates the device context
• Streaming object’s persistent data to or from a
storage medium (disk reading/writing) (Graphical Drawing)
– Diagnostic & Debugging support ? CGdiObject: Base class for various drawing

? All its functionality is inherited by any objects (brushes, pens, fonts, etc.)
classes derived from it ? CMenu: Encapsulates menu management

? CCmdTarget: Encapsulates message passing


? CCmdTarget also parent of:
process & is parent of:
– CWinThread: Defines a thread of execution and
– CWnd: Encapsulates many important windows
is the parent of:
functions and data members
• CWinApp: Most important class dealt with in MFC
– Example: m_hWnd stores the window’s handle applications:
– Base class all windows are derived from • Encapsulates an MFC application
– Most common: • Controls following aspects of Windows programs:
• CFrameWindow: Can contain other windows – Startup, initialization, execution, shutdown
– An application should have one CWinApp object
– ("normal" kind of window we've used)
– When instantiated, application begins to run
• CView: Encapsulates process of displaying and
– CDocument
interacting with data
• Encapsulates the data associated with a program
• CDialog: Encapsulates dialog boxes

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

MFC Global Functions-- Some Important Global Functions


? Not members of any MFC class ? AfxAbort () -- uconditionally terminate an app
? Begin with Afx prefix (Application ? AfxBeginThread() -- Create & run a new thread
FrameworKS) ? AfxGetApp() -- Returns a pointer to the
application object
? Independent of or span MFC class hierarchy
? AfxGetMainWnd() -- Returns a pointer to
? Example: application’s main window
– AfxMessageBox() ? AfxGetInstanceHandle() -- Returns handle to
– Message boxes are predefined windows applications’s current instance
– Can be activated independently from the rest of ? AfxRegisterWndClass() -- Register a custom
an application WNDCLASS for an MFC app

A Minimal MFC Program Message Processing under MFC


(App/Window Approach) ? Like API programs, MFC programs must handle
? Simplest MFC programs must contain two classes messages from Windows
derived from hierarchy: ? API mechanism: big switch/case statement
– 1. An application class derived from CWinApp ? MFC mechanism: "message maps" (lookup
• Defines the application tables)
• provides the message loop ? Table entries:
– 2. A window class usually derived from – Message number
– Pointer to a derived class member message-processing
CFrameWnd function
• Defines the application's main window • These are members of CWnd
? These & other MFC classes brought in by using • You override the ones you want your app to respond to

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

STEPS IN WRITING A DECLARATIONS (.h)


SIMPLE MFC PROGRAM 1. Declare a window class derived from
CFrameWnd (e.g., CMainWin)--
(App/Window Approach) ? Class Members:
– The constructor
– Message-processing function declarations for messages
the application will 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
overriding functions in the application
• Should be last class member declared

2. Declare an application class derived from IMPLEMENTATION (.CPP)


CWinApp (e.g., CApp)-- 1. Define constructor for class derived from
CFrameWnd (CMainWin)
? Must override CWinApp's InitInstance()
? Should call member function Create() to create the
virtual function: window
– Called each time a new instance of application ? Does what CreateWindow() does in API
is started
– i.e., when an object of this class is instantiated
2. Define message map for class derived from
– Purpose is for application to initialize itself
CFrameWnd (CMainWin)--
– Good place to put code that does stuff that has BEGIN_MESSAGE_MAP(owner, base)
to be done each time program starts List of "message macros" [e.g., ON_WM_CHAR()]
END_MESSAGE_MAP()

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. Create an instance of the app class (CApp)


? After WinApp::Run() returns:
? Causes AfxWinMain() to execute
– (i.e., when the WM_QUIT message is received)
– It's now part of MFC [WINMAIN.CPP]
? AfxWinMain() does the following: ? AfxWinTerm() is called--
– which cleans up and exits
– Calls AfxWinInit()--
• which calls AfxRegisterClass() to register window class
– Calls CApp::InitInstance() [virtual function
overridden in 4 above]--
• which creates, shows, and updates the window
– Calls CWinApp::Run() [In THRDCORE.CPP]--
• which calls CWinThread::PumpMessage()--
• which contains the GetMessage() loop

PROG1 Example MFC Steps in Creating and Building an MFC


Application like PROG1 “manually”
Application: 1. “File | New”
– Specify an empty Win32 project as in previous examples
? Just creates a skeleton frame window 2. “Project | New | C++” or “Project | Add to existing | C++”
– Enter or copy/paste .cpp file text (e.g., PROG1.CPP)--see
IMPLEMENTATION above
3. “File | New | C++ header” or “Project | Add to existing | header”
– Enter or copy/paste .h file text (e.g., PROG1.H)--see
DECLARATION above
4. “Project | Properties | General” (with prog1 highlighted in
Solution Explorer window):
– From “Use of MFC”, choose:
– "Use MFC in a Shared DLL"
5. Build the project as usual

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy