11 introduction to mfc

37
Introduction to Microsoft Foundation Classes

Upload: rajivpoplai

Post on 29-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 1/37

Introduction to Microsoft

Foundation Classes

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 2/37

GUID, FPGDST 2004-2005, C-DAC

Background on Object Oriented

Programming

Abstraction

Encapsulation Inheritance

Polymorphism

Modularity

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 3/37

GUID, FPGDST 2004-2005, C-DAC

Design Goals of MFC

Deliver a real-world application

 ± Using C++ and Object Oriented techniques

Deliver an small and fast application framework 

 ± To simplify Windows development

 ± Allow developers to leverage their existing knowledge

of Windows

 ± To build large-scale applications that utilize the latest

Windows enhancements, such as OLE and ODBC

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 4/37

GUID, FPGDST 2004-2005, C-DAC

Delivering a real-world application

No new programming concepts

 ± Extends the object-oriented programming model that

was already used by Windows

 ± Expresses their concepts in standard C++ idioms

Subset of C++ is defined in MFC

 ± However, no restrictions were placed on code that a

 programmer could write

 ± you can use any feature of C++ you want to

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 5/37

GUID, FPGDST 2004-2005, C-DAC

Simplifying the Windows API..

API functions are grouped into logical units. ± All functions related to device context appear in one

class and all functions related to a generic window

appear in one«

Safely hides away the messy details of Windows programming

 ± such as registering windows classes, window anddialog procedures, and message routing

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 6/37

GUID, FPGDST 2004-2005, C-DAC

..S

implifying the Windows API Strong typing and data-encapsulation features of 

C++ is used ± To make development mechanically easier 

 ± e.g. Every windows message uses two parameters,WPARAM and LPARAM. However the actual valuesstored in the message maybe any number of things:

pointers, POINT structures, windows handles, etc.

 ± To retrieve the parameters, the C/SDK programmer 

needs to decode the parameters into proper type of values

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 7/37

GUID, FPGDST 2004-2005, C-DAC

Using the knowledge you already

have

The class hierarchy and naming conventions on

the underlying API naming conventions

 ± To minimize the relearning curve for experienced

developers

So converting a standard program written using

SDK to MFC is fairly simple

 ± Even conversion might not be required

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 8/37

GUID, FPGDST 2004-2005, C-DAC

A Firm Foundation The Windows API keeps getting bigger and bigger 

 ± As it continues to collect new material

 ± Microsoft keeps adding new APIs to Windowsincluding OLE and ODBC

Initial framework does not support every newWindows API

 ± Was designed so that it could be readily extended.Every new API introduces a new learning curve

 ± MFC eases things by encapsulating the details of anAPI in a set of classes

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 9/37

GUID, FPGDST 2004-2005, C-DAC

Application Framework.. Collection of classes which work in a well defined

manner 

Defines fixed steps to create an application

Developer does not have much control

Provides hooks for plugging in developer code

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 10/37

GUID, FPGDST 2004-2005, C-DAC

..Application Framework

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 11/37

GUID, FPGDST 2004-2005, C-DAC

A Tour of MFC General-Purpose classes

 ± CObject, CException

Windows API classes

 ± CCmdTarget, CWinThread, CWinApp

Application framework classes

 ± CDocument, CView

High Level abstractions

 ± ScrollView, Splitter 

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 12/37

GUID, FPGDST 2004-2005, C-DAC

Important Classes CWinApp

CWnd

CFrameWnd

CView

CDocument

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 13/37

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 14/37

GUID, FPGDST 2004-2005, C-DAC

CWnd Represents a window

Used for display purposes

Also receives events

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 15/37

GUID, FPGDST 2004-2005, C-DAC

CFrameWnd Derived from CWnd

Contains menus, toolbars, status bars and

view window

Constitutes main window of application

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 16/37

GUID, FPGDST 2004-2005, C-DAC

CView Represents view window

Used in Document/View

Created as a child of CFrameWnd

Used to display contents of document

Receives events

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 17/37

GUID, FPGDST 2004-2005, C-DAC

CD

ocument Represents document object

Used in Document/View

Created as an independent object

Tightly bound to view object

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 18/37

GUID, FPGDST 2004-2005, C-DAC

Application WithoutD

oc/View Minimum application

Event handling

Window painting

Menus

Dialog boxes

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 19/37

GUID, FPGDST 2004-2005, C-DAC

Basic Windows

Support

Windows sits between the hardware and the

applications it is hosting

 ± Whenever anything happens that is of interest to one of the applications, Windows informs the application

A substantial amount of code is required by a

windows application to receive and process events

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 20/37

GUID, FPGDST 2004-2005, C-DAC

The tasks to be accomplished The Windows application needs to set up a message

handler and plug it into Windows (via RegisterClass()API)

 ± so that Windows knows where to go and get messageshandled for a specific application

Windows needs to keep track of specific instances of anapplication

The application asks Windows for the next event in themessage queue, dispatches the message to theappropriate message handler, and then gets the next one

This activity continues till the application ends

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 21/37

GUID, FPGDST 2004-2005, C-DAC

Requirements of a Windows

Program.. The program must contain a WinMain() function.

 ± Just as every C program requires a main() function, every

 ± WinMain() acts as the entry point for the program. It is through the WinMain() function that the application receives

from Windows the information necessary for it to run

The application must register at least one windowclass to serve as the main window ± It is the window¶s job to present the user interface on the

screen

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 22/37

GUID, FPGDST 2004-2005, C-DAC

..Requirements of a Windows

Program Most applications require some initialization and setup

This initialization usually takes two forms

 ± application-specific ± instance-specific initialization

 ± The program needs to provide any necessary initialization

steps

The application must provide a message handler  ± The function of the window procedure is to handle messages

 ± At the very least, the window procedure must handle the

WM_DESTROY message

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 23/37

GUID, FPGDST 2004-2005, C-DAC

Sample - Minimum Application

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 24/37

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 25/37

GUID, FPGDST 2004-2005, C-DAC

A minimum Application

#include <afxwin.h>

//Define an application class derived from CWinApp

class CGenericApp : public CWinApp {

 public:

virtual BOOL InitInstance();};

class CGenericWindow : public CFrameWnd {

 public:

CGenericWindow() {

Create(NULL, ³Generic´);}

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

DECLARE_MESSAGE_MAP()

};

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 26/37

GUID, FPGDST 2004-2005, C-DAC

A minimum Application

BEGIN_MESSAGE_MAP(CGenericWindow, CFrameWnd)

ON_WM_LBUTTONDOWN()

END_MESSAGE_MAP()

Void CGenericWindow::OnLButtonDown(UINT nFlags, Cpoint point) {

MessageBox( ³Left mouse button pressed«´, NULL, MB_OK);

}

BOOL CGenericApp::InitInstance() {

m_pMainWnd = new CGenericWindow();

m_pMainWnd->ShowWindow(m_nCmdShow);

m_pMainWnd->UpdateWindow();

return TRUE;

}

CGenericApp GenericApp;

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 27/37

GUID, FPGDST 2004-2005, C-DAC

Analysis..

This program looks different from a C/SDK program

 ± The WinMain() function is gone, and

 ± There is no code that seems to be registering the window

classes

There is no message handling function anywhere

 ± It seems the message loop hasn¶t been created

Most of the implementation details have been hidden

from the programmer  ± The framework does most of the job for the programmer and

have been hidden inside macros likeBEGIN_MESSAGE_MAP

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 28/37

GUID, FPGDST 2004-2005, C-DAC

..Analysis

WinMain() is part of the application framework andMFC¶s

 ± WinMain() does a very good job initializing the variables,creating the window and setting up the message loop

The function InitInstance() is called by the WinMain()

 ± Handles the creation of the window and registers thewindow classes too

MFC¶s message handling still uses the same functions

GetMessage(), TranslateMessage() andDispatchMessage()

 ± But at the lowest level because these functions have beenencapsulated

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 29/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

To start a new MFC project«

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 30/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

There are many options in the following dialogs, but for 

the time being, choose

 ± Choose ³Dialog Based´ & Check ³Windows Sockets´

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 31/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

Now you have

 ± C++ files & differentclasses for your project

 ± Basic GUI Resourcesfile

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 32/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

The Resource Editor 

Visual Studio IDE provides us with

WYSIWYG editor to generate the

definition file for us.

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 33/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

Know the class

structure

The application class

The Dialog¶s (GUI) class

Start your code here

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 34/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

Adding components in the generated dialog

Select UI

controls

here

To view/add/edit

messages,

handler functions,

member variables by

ClassWizard

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 35/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

View/Add Event Handlers

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 36/37

GUID, FPGDST 2004-2005, C-DAC

The IDE

Adding event handler functions

 ± After double clicking on the button, the IDE helps you toregister a listener function that handles BN_CLICKED

message

 Add your 

codes for 

handling click

event here

8/9/2019 11 Introduction to MFC

http://slidepdf.com/reader/full/11-introduction-to-mfc 37/37

GUID, FPGDST 2004-2005, C-DAC

References

MSDN Online

Beginning Visual C++ 6

 ± Ivor Horton, Wrox Publication

Learn Microsoft Visual C++ 6.0 Now

 ± Chuck Sphar, Microsoft Press

Samples ± min

 ± appwiz