Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Foundation Classes MFC

Similar presentations


Presentation on theme: "Microsoft Foundation Classes MFC"— Presentation transcript:

1 Microsoft Foundation Classes MFC
CS Visual Programming Microsoft Foundation Classes MFC Lecture 3

2 Microsoft Foundation Classes
Windows API was not written with Visual C++ or even C++ in mind, since it was written before C++ came into general use. The API functions don't handle or recognize class . MFC: A set of predefined classes upon which Windows programming with Visual C++ is built. These classes represent an object oriented approach to Windows programming that encapsulates the Windows API.

3 Using the MFC library classes offers an alternative to using Win32 API  functions. The following diagram illustrates how a Visual C++ Windows  application can use either the Win32 API or MFC (or both) to write a program  that tells the computer hardware what has to be done:

4 There are about 200 MFC classes (versus more than 2000 API functions).
They provide a framework upon which to build Windows applications. They encapsulate most of the Win32 API in a set of logically organized classes. Some of the characteristics of MFC are– They offer the convenience of REUSABLE CODE. Many tasks common to all Windows apps are provided by MFC. They produce smaller executables. They can lead to faster program development. MFC Programs must be written in C++ and require the use of classes.

5 MFC Notation All the classes in MFC have names beginning with C, such as CDocument or Cview Data members of an MFC class are prefixed with m_. We'll also follow this convention in this class.

6 An MFC Application

7 The first task to be done in any MFC application is to create a window and the MFC application running that window. MFC provides two important classes CWinApp and CFrameWnd, which can be used to create a window & the application. CWinApp provides the application level functionalities. CFrameWnd provides the functionalities related to GUI. Both classes are derived from CCmdTarget which in turn is derived from CObject. CCmdTarget is created with the capability to handle windows messages, which is referred as Message Maps.

8 The Application Class The class CWinApp is fundamental to any Windows program written using MFC. An object of this class includes everything necessary for starting, initializing, running and closing the application. The first thing that we need to do to produce our application is to derive our own application class from CWinApp and modify it according to our application needs. A MFC Windows application starts at the WinMain() function. This function is hidden by MFC, i.e. it is implemented by MFC for you. The MFC program entry point is the member function CWinApp::InitInstance() – compare to Win32 InitInstance().

9 class COurApp: public CWinApp
{ public: virtual BOOL InitInstance() // Construct a window object in the free store m_pMainWnd = new COurWnd; m_pMainWnd->ShowWindow(m_nCmdShow); // ...and display it return TRUE; } }; InitInstance(): This function is defined as a virtual function in the base class, so it's not a new function in our derived class. All the other data and function members that we need in our class will be inherited from CWinApp without changes. This function will be called by the WinMain() function that's automatically supplied by the MFC library.

10 The Window Class Our MFC application will need a window as the interface to the user, referred to as a frame window. We will derive a window class for our application from the MFC class CFrameWnd, which is designed specifically for this purpose. Since CFrameWnd provides everything for creating and managing a window for our application, all we need to add to our derived window class is a constructor to suit the application context

11 class COurWnd: public CFrameWnd
{ public: // Class constructor COurWnd() Create(0, "Our Dumb MFC Application"); } The first argument value for the Create() function, 0, specifies that we want to use the base class default attributes for the window The second argument specifies the window name which will be used in the window title bar.

12 The Document/View Concept
The Document/View architecture is the foundation used to create applications based on the Microsoft Foundation Classes library. The MFC document/view architecture includes a combination of a document, in which data is stored, and a view, which has privileged access to the data. The document/view architecture separates the storage and maintenance of data from the display of data.

13 This separation facilitates development of applications that display multiple views of a single set of data simultaneously. When the application user changes the data in one view, the architecture handles the task of notifying the other views that they must update to display the modified data. The parts that compose the Document/View architecture are a frame, one or more documents, and the view. Put together, these entities make up a usable application.

14 What is a Document? A document is similar to a bucket. It can be used to hold or carry water and that water can be retrieved when needed. For a computer application, a document holds the user's data. For example, after working on a text processor, the user may want to save the file. Such an action creates a document and this document must reside somewhere. In the same way, to use an existing file, the user must locate it, open it, and make it available to the application. These two jobs and many others are handled behind the scenes as a document. It could be the data for a game, a geometric model, a text file, a collection of data on the distribution of orange trees in California or, indeed, anything you want. The term 'document' is just a convenient label for the application data in your program, treated as a unit. A document in your program will be defined as an object of a document i.e. will be derived from the class CDocument in the MFC library, and you'll add your own data members to store items that your application requires, and member functions to support processing of that data.

15 Document Interfaces You have a choice as to whether your program deals with just one document at a time, or with several. Single Document Interface SDI: Supported by the MFC library for programs that require only one document to be open at a time. A program using thisinterface is referred to as an SDI application. Multiple Document Interface MDI: For programs that need several documents of one type to be open at one time your program can also be organized to handle documents of different types simultaneously.

16 What is a View? A view always relates to a particular document object.
A view is an object which provides a mechanism for displaying some or all of the data stored in a document. It defines how the data is to be displayed in a window and how the user can interact with it. Similar to the way that you define a document, you'll define your own view class by deriving it from the MFC class CView. Note that a view object and the window in which it is displayed are distinct. The window in which a view appears is called a frame window. A view is actually displayed in its own window that exactly fills the client area of a frame window.

17 The general relationship between a document, a view and a frame window is illustrated here:
A document object can have multiple view objects associated with it. Each view object can provide a different presentation or subset of the same document data. If you were dealing with text, for example, different views could be displaying independent blocks of text from the same document. For a program handling graphical data, you could display all of the document data at different scales in separate windows, or in different formats, such as a textual representation of the elements that form the image.

18 Document Templates Document template manages the document objects in your program, as well as the windows And views associated with each of them. There will be one document template for each different Kind of document that you have in your program. If you have two or more documents of the Same type, you only need one document template to manage them. Document objects and frame Window objects are created by a document template object. A view is created by a frame Window object. The document template object itself is created by the application object that is Fundamental to any MFC application, as we saw in the last example.

19

20 Your Application and MFC
The four basic classes that are going to appear in virtually all your Windows applications: The application class, CMyApp. The frame window class, CMyWnd. The view class, CMyView, which will define how data contained in CMyDoc is to be displayed in the client area of a window created by a CMyWnd object. The document class, CmyDoc, defining a document to contain the application data.


Download ppt "Microsoft Foundation Classes MFC"

Similar presentations


Ads by Google