26
Lecture 4 1 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Embed Size (px)

Citation preview

Page 1: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 1

Introduction to MicrosoftWindows MFC Programming:

the Application/WindowApproach

Page 2: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 2

MFC Windows Programming(App/Window Approach)

• MFC stands for “The Microsoft Foundation Class Library”

• Object oriented interface to windows.• A Hierarchy of C++ classes designed to facilitate

Windows programming• An alternative to using Win32 API functions• A Visual C++ Windows application can use either

Win32 API, MFC, or both

Page 3: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 3

Relationship between Windows MFC and Win32 API Programming

Page 4: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 4

Microsoft Foundation Classes• About 200 MFC classes (versus 2000+ API functions )• Also called “Application Frame work”

– Provide a framework upon which to build Windows applications

• Encapsulate most of the Win32 API in a set of logically organized classes

• MFC organizes the WIN32 API. • it groups related functionality into a class. • Wraps the windows API and windows objects with classes

Page 5: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 5

MFC Characteristics

1. Convenience of reusable code:– Many tasks common to all Windows apps are

provided by MFC– Our programs can inherit and modify this

functionality as needed– We don't need to recreate these tasks

2. Produce smaller executables:– Typically 1/3 the size of their API counterparts

Page 6: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 6

MFC Characteristics (contd)3. Can lead to faster program development:

– Make creation of windows application with C++ easy– Make access to Windows API features easier

4. MFC Programs must be written in C++– require the use of classes

– Programmer must have good grasp of:• How classes are declared, instantiated, and used• Encapsulation• Inheritance• Polymorphism--virtual functions

Page 7: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 7

MFC Notations • Classes names begins with ‘C’

– CFrameWnd– CView

• Variable names begin with ‘m_’– m_pFrameWnd

• Functions name begins with Afx– AfxMessageBox()

Page 8: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 8

MFC Class Hierarchy• Two categories of classes

– Derived from CObject– Not Derived from CObject

• CObject– At top of hierarchy ("Mother of all classes")– Provides features like:

• Serialization– Streaming object’s persistent data to or from a storage medium (disk

reading/writing)

• Diagnostic & Debugging support

– All its functionality is inherited by any classes derived from it

Page 9: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 9

Important Derived Classes—

• CFile: Support for file operations

• CDC: Encapsulates the device context (Graphical Drawing)

• CGdiObject: Base class for various drawing objects (brushes, pens, fonts, etc.)

• CMenu: Encapsulates menu management

Page 10: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 10

• CCmdTarget: Encapsulates message passing process& is parent of:

– CWnd: Encapsulates many important windows functions and data members

– Example: m_hWnd stores the window’s handle– Base class all windows are derived from– Most common:

• CFrameWindow: Can contain other windows – ("normal" kind of window we've used)

• CView: Encapsulates process of displaying and interacting with data

• CDialog: Encapsulates dialog boxes

Page 11: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 11

• CCmdTarget also parent of:– CWinThread: Defines a thread of execution and is the

parent of:• CWinApp: Most important class dealt with in MFC

applications:

– Encapsulates an MFC application– Controls following aspects of Windows programs:

• Startup, initialization, execution, shutdown

• An application should have one CWinApp object

• When instantiated, application begins to run

– CDocument• Encapsulates the data associated with a program

Page 12: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 12

Not Derived from CObject

• Provide valuable additional services for MFC:– OLE Classes– Simple Data Type Classes

• CImage

• CRect

• CString etc.

– Synchronization classes• CSemaphore

• CMutex

Page 13: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 13

• Applications can also call API functions directly– Use Global Scope Resolution Operator, for

• example:– ::UpdateWindow(hWnd);

• Usually more convenient to use MFC member functions

Page 14: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 14

A Minimal MFC Program

• Simplest MFC programs must contain two classes derived from hierarchy:

1. An application class derived from CWinApp• Defines the application

• provides the message loop

• Three common methods are– InitApplication();

– InitInstance();

– Run();

These methods created automatically when object is created.

Page 15: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 15

2. A window class usually derived from CFrameWnd

– Defines the application's main window– Referred as frame window– Tasks:

• Creation of window• Management of windowIt creates window and attach it to our program

• These & other MFC classes brought in by using #include <Afxwin.h>

Page 16: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 16

• #include <afxwin.h>

class CMyApp : public CWinApp

{

public:

virtual BOOL InitInstance();

};

Page 17: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 17

class CMainFrame : public CFrameWnd {

public: CMainFrame(){

Create(NULL, "MFC Fundamentals"); }

};

BOOL CMyApp ::InitInstance() {

m_myWnd * CMainFrame ; m_myWnd = new CMainFrame; m_myWnd ->ShowWindow(SW_NORMAL); m_pMainWnd= m_myWnd ;return TRUE;

}CMyApp a;

Page 18: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 18

Message Processing under MFC

• Like API programs, MFC programs must handle messages from Windows

• API mechanism: big switch/case statement

• MFC mechanism: "message maps" (lookup tables)

Page 19: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 19

• Messages in an application can be send in 2 ways– Directly– Queued

• Directly– Message queue and message loop both are

by passed – Windows directly call WndProc()– ::SendMessage

• Queued– ::PostMessage

Page 20: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 20

System Defined Messages• Windows uses these messages

• Each of the system defined messages has – unique numeric identifier– Corresponding identifier macro definition

• System defined message macro prefix– BM Button control Messages– CB Combo Box control Messages– EM Edit Box control Messages– LB List Box control Messages– WM General Window Messages

Page 21: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 21

Message Handler• Message handler methods receive notification from the

application object when some event occurs

• Three types of messages processed in Message handler – Windows Messages

– Control Notification Messages

– Command Messages

• Windows Messages– Send by window to application

– Starts with WM_*

– WM_COMMAND exception

Page 22: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 22

• Control Notification Messages– Send from child window– Control has no message queue and loop– Whatever activity notify directly to parent– Use message loop and queue of parent

• Command Messages– Include WM_COMMAND – Notification from user interface object like

• menu buttons

• toolbar buttons

Page 23: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 23

Message Maps

• Mechanism for handling messaging with in a process• Object Oriented alternative to “switch-case” of

window procedure• Table entries:

– Message number

– Pointer to a derived class member message-processing function

• These are members of CWnd

• You override the ones you want your app to respond to

• Known as “Message handler”

Page 24: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 24

Message Mapping

• Programs must:– Declaration

• DECLARE_MESSAGE_MAP()

– Implementation• BEGIN_MESSAGE_MAP( FC_Tutorial_Window,

CFrameWnd)      ON_WM_LBUTTONDOWN() //Macro to map the left button click to the handlerEND_MESSAGE_MAP()

Page 25: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 25

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.

Page 26: Lecture 41 Introduction to Microsoft Windows MFC Programming: the Application/Window Approach

Lecture 4 26