Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Microsoft Windows MFC Programming: the Application/Window Approach Lecture 4."— Presentation transcript:

1 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 Lecture 4

3 Relationship between Windows MFC and Win32 API Programming
Lecture 4

4 Microsoft Foundation Classes
About 200 MFC classes (versus 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 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 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 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() Lecture 4

8 MFC Class Hierarchy Two categories of classes CObject
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 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 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 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 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 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 Lecture 4

14 Simplest MFC programs must contain two classes derived from hierarchy:
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. 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 window It creates window and attach it to our program These & other MFC classes brought in by using #include <Afxwin.h> Lecture 4

16 #include <afxwin.h> class CMyApp : public CWinApp { public:
virtual BOOL InitInstance(); }; 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; 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) Lecture 4

19 Messages in an application can be send in 2 ways
Directly Queued Message queue and message loop both are by passed Windows directly call WndProc() ::SendMessage ::PostMessage 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 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 Send by window to application Starts with WM_* WM_COMMAND exception 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 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” Lecture 4

24 Message Mapping Programs must: Declaration Implementation
DECLARE_MESSAGE_MAP() Implementation BEGIN_MESSAGE_MAP( FC_Tutorial_Window, CFrameWnd)       ON_WM_LBUTTONDOWN() //Macro to map the left button click to the handler END_MESSAGE_MAP() 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. Lecture 4

26 Lecture 4


Download ppt "Introduction to Microsoft Windows MFC Programming: the Application/Window Approach Lecture 4."

Similar presentations


Ads by Google