Microsoft foundation classes
Download
1 / 60

Chapter 10 - PowerPoint PPT Presentation


  • 286 Views
  • Updated On :

Microsoft Foundation Classes Objectives In this chapter you will learn: About Microsoft Foundation Class programming How to write basic MFC programs How to work with resources About the CString class How to create dialog-based applications How to work with message maps

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 10' - benjamin


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Microsoft foundation classes l.jpg
Microsoft Foundation Classes

Microsoft Visual C++ .NET Chapter 10


Objectives l.jpg
Objectives

In this chapter you will learn:

  • About Microsoft Foundation Class programming

  • How to write basic MFC programs

  • How to work with resources

  • About the CString class

  • How to create dialog-based applications

  • How to work with message maps

  • How to build an application framework with the MFC Application Wizard

Microsoft Visual C++ .NET Chapter 10


Microsoft foundation classes3 l.jpg
Microsoft Foundation Classes

  • The Microsoft Foundation Classes, or MFC, is a class library that assists programmers in creating Windows-based applications

  • Perhaps two of the most important aspects of MFC programming are:

    • MFC adds object-oriented programming capabilities to Windows API programming

    • MFC encapsulates the Windows API into a logically organized hierarchy

Microsoft Visual C++ .NET Chapter 10


Microsoft foundation classes4 l.jpg
Microsoft Foundation Classes

  • One of Microsoft’s main goals in designing MFC was to create a C++ object-oriented class library for building Windows API applications

  • Prior to MFC, programmers had to design their programs so that they conformed to the C language requirements of the Windows API

  • This means that they could not use the object-oriented capabilities of C++, which are not found in C

  • Visual C++ is designed to make writing MFC programs a relatively easy process

Microsoft Visual C++ .NET Chapter 10


Microsoft foundation classes5 l.jpg
Microsoft Foundation Classes

  • When you use Visual C++ to write an MFC program, you first use the MFC Application Wizard to walk you through the steps involved in creating an MFC application

  • After running MFC Application Wizard, Visual C++ provides something called the Microsoft Foundation Class framework, or MFC framework for short, which is basically a skeleton application created from MFC classes that you can use as a basis for your program

Microsoft Visual C++ .NET Chapter 10


Microsoft foundation classes6 l.jpg
Microsoft Foundation Classes

  • A hook is a location in a program where a programmer can insert code that adds functionality

  • Visual C++ provides plenty of comments to help you find the hooks

Microsoft Visual C++ .NET Chapter 10


Mfc class library l.jpg
MFC Class Library

  • The MFC class library consists of two major sections:

    • The MFC class hierarchy

    • Global functions and macros

Microsoft Visual C++ .NET Chapter 10


Mfc class hierarchy l.jpg
MFC Class Hierarchy

  • The simple class hierarchy examples you saw in Chapter 8 were relatively small

  • In contrast, the MFC class hierarchy is surprisingly large

  • The MFC class library contains over two hundred classes

  • Figure 10-5 shows a partial listing of the class hierarchy, highlighting some of the more important classes

Microsoft Visual C++ .NET Chapter 10


Partial listing of the mfc class hierarchy l.jpg
Partial Listing of the MFC Class Hierarchy

Microsoft Visual C++ .NET Chapter 10


Mfc class hierarchy10 l.jpg
MFC Class Hierarchy

  • Most classes in the MFC class hierarchy derive from the CObject base class

  • The most important branch of the MFC class hierarchy is the CCmdTarget class, which encapsulates the messaging features of the Windows API

  • The CWinApp class, also known as the application class, is responsible for initializing, starting, running, and stopping an MFC windows application

Microsoft Visual C++ .NET Chapter 10


Mfc class hierarchy11 l.jpg
MFC Class Hierarchy

  • The CWnd class encapsulates the various Windows API functions, data types, and other code used for creating and instantiating windows

  • Derived from the CWnd class are more specialized types of window classes such as the CFrameWnd class, which creates a standard type of window known as a frame window, and the CDialog class for creating dialog boxes

Microsoft Visual C++ .NET Chapter 10


Global functions l.jpg
Global Functions

  • If a function is not a member of an MFC class, then it is a global function that is available to all MFC classes, regardless of their position in the MFC class hierarchy

  • All MFC global functions begin with a prefix of Afx

  • Figure 10-6 shows an example of a message box created with the AfxMessageBox() function

Microsoft Visual C++ .NET Chapter 10


Macros l.jpg
Macros

  • A programming element that is commonly used in MFC programming is a macro

  • A macro represents C++ code, constants, and other programming elements and is defined using the #define preprocessor directive

  • A macro in C++ is a name that represents C++ code and other programming elements that you would like to execute simply by calling the macro name, similar to the way you call a function name

Microsoft Visual C++ .NET Chapter 10


Macros14 l.jpg
Macros

  • During preprocessing, a macro name is replaced by the code and other programming elements that it represents

  • This process is similar to using the inline keyword to request that the compiler replace calls to a function with the function definition wherever in a program the function is called

  • Macro names in the MFC library are in all uppercase letters

Microsoft Visual C++ .NET Chapter 10


Mfc notation l.jpg
MFC Notation

  • All MFC class names begin with C

  • Additionally, data members of MFC classes are prefixed with m_

  • Figure 10-7 shows an example of the MFC Class Wizard when you add an MFC class named CCalculator

Microsoft Visual C++ .NET Chapter 10


Basic mfc programs l.jpg
Basic MFC Programs

  • You can easily create the MFC framework for an MFC program using the MFC Application Wizard

  • The classes that turn a standard C++ program into an MFC program derive from MFC classes

  • By default, Win32 application projects do not support MFC programming

  • In order to enable MFC support in a Win32 application project, you must select the Use MFC in a Shared DLL setting in the Use of MFC combo box in the General category of the Property Pages dialog box

Microsoft Visual C++ .NET Chapter 10


The application class l.jpg
The Application Class

  • An application class is the starting point of any MFC application

  • You derive an application class from the CWinApp class

  • The application class object you instantiate in an MFC program represents the application as a whole

  • When you derive an application object, you must:

    • Override the virtual InitInstance() function

    • Instantiate a global object of your application class

Microsoft Visual C++ .NET Chapter 10


The application class18 l.jpg
The Application Class

  • The InitInstance() function is called by the inherited WinMain() function each time a new instance of your MFC program starts

  • You can instantiate only one global application class object, and it is usually instantiated within the application class’s implementation file

  • Figure 10-10 in the text shows an example of a basic application class named CbasicApp

Microsoft Visual C++ .NET Chapter 10


The windows class l.jpg
The Windows Class

  • The class you will use in this section is the CFrameWnd class, which creates a simple window with a frame, title bar, control menu, and control buttons

  • One task that is required for all window classes is calling the inherited Create() function from the class constructor

  • The Create() function creates the window itself when an object of the window class is instantiated

Microsoft Visual C++ .NET Chapter 10


The windows class20 l.jpg
The Windows Class

  • In standard Windows API programming, you use the ShowWindow() function to display a window

  • MFC programming also uses a ShowWindow() function to display windows

  • It is important that you understand that in an MFC Program, the ShowWindow() function is not called from the window class

  • Instead, you call the ShowWindow() function from the application class’s InitInstance() function using an instantiated object of the window class

Microsoft Visual C++ .NET Chapter 10


The windows class21 l.jpg
The Windows Class

  • An important concept to understand is that m_pMainWnd object is not the window itself

  • All objects that display windows are not the windows themselves

  • The window is only a visual representation of the object

  • To add a window class to the MFC Calculator program follow the instructions on pages 533 and 534 of the textbook

Microsoft Visual C++ .NET Chapter 10


Resources l.jpg
Resources

  • A resource is a graphical user interface element or type of stored information that is used by a Windows application

  • Figure 10-16 in the text lists the standard Windows API resources

  • Resources are defined in special files called resource scripts

  • Resource scripts have an extension of .rc and are written in C preprocessor language

Microsoft Visual C++ .NET Chapter 10


Resources23 l.jpg
Resources

  • A resource ID is an integer constant declared with the #define preprocessor directive and is used for programmatically referring to a resource

  • It is common practice to declare all resource constants in an interface file named resource.h in order to make it easier to reference them in your program

  • Visual C++ automatically creates a resource script when you run either the MFC Application Wizard or the Add Resource Wizard

Microsoft Visual C++ .NET Chapter 10


Add resource wizard l.jpg
Add Resource Wizard

  • You use the Add Resource Wizard to add new resources to an MFC project

  • You start the Add Resource Wizard by selecting the Add Resource command from the Project menu to display the Add Resource dialog box

  • Figure 10-17 shows an example of the Add Resource dialog box

Microsoft Visual C++ .NET Chapter 10


Resource editors l.jpg
Resource Editors

  • Resource editors allow you to quickly create and modify resources in a graphical environment

  • You can still edit a resource file’s C preprocessor code, though Visual C++ does an excellent job of writing the code for you

  • Figure 10-19 in the text shows the C preprocessor code that represents the same dialog box

  • You can also see the Toolbox in Figure 10-18

Microsoft Visual C++ .NET Chapter 10


Resource view l.jpg
Resource View

  • Resource View contains folders representing the different resource types

  • You can use Resource View to quickly open a resource in a resource editor

  • To view a resource in its editor, expand its resource folder and double-click the resource name

  • Figure 10-20 in the text shows the IDE after expanding the Toolbar folder in the Resource View tab and double-clicking the IDR_MAINFRAME resource

Microsoft Visual C++ .NET Chapter 10


Properties window l.jpg
Properties Window

  • The Properties window in Visual C++ (and other Visual Studio tools) is used for managing the properties of various elements in a project, including the properties of resources

  • Different types of resources have different properties available in their Properties window

  • Figure 10-21 shows a portion of the Properties window for a button in the Dialog Box editor

Microsoft Visual C++ .NET Chapter 10


Properties window for a button in the dialog box editor l.jpg
Properties Window for a Button in the Dialog Box Editor

Microsoft Visual C++ .NET Chapter 10


Properties window29 l.jpg
Properties Window

  • At the top of the Properties window is the Object name combo box, which displays the name of the currently selected object or objects

  • Below the Object name combo box is the Properties window toolbar, which includes:

    • A Categorized button, which displays all of an object’s properties by category

    • An Alphabetic button, which displays all of an object’s properties alphabetically

    • A Properties button, which displays an object’s properties

    • A Property Pages button, which displays the project’s Property Pages dialog box

Microsoft Visual C++ .NET Chapter 10


Properties window30 l.jpg
Properties Window

  • An Events button also appears for any selected objects that have associated events

  • Visual C++ treats almost anything, including class members, as a property

  • If you need to make any changes to the basic declarations (function parameters, the data types of member variables, and so on) in your class, you have two choices

  • You can either make the changes manually in the header and source files, or you can use the Properties window

Microsoft Visual C++ .NET Chapter 10


Properties window31 l.jpg
Properties Window

  • In order to display a class member’s properties in the Properties window, you must select the class member in Class View

  • Figure 10-22 shows a portion of the Properties window for the InitInstance() member function in the Ccalculator class

  • Notice in Figure 10-22 that InitInstance() is selected in Class View

Microsoft Visual C++ .NET Chapter 10


Properties window32 l.jpg
Properties Window

  • The Properties window toolbar includes two additional buttons, Messages and Overrides, when one of the following conditions is true:

    • When the Code Editor window is the active window in the IDE and it is opened to a class interface or implementation file

    • When Class View is the active window in the ID, and you have selected a class icon in Class View

Microsoft Visual C++ .NET Chapter 10


A member function in the properties window l.jpg
A Member Function in the Properties Window

Microsoft Visual C++ .NET Chapter 10


The cstring class l.jpg
The CString Class

  • The CString class is used for manipulating strings in MFC programs, and works in much the same way as the string class

  • You create a string variable with the CString class using a statement similar to CString myString;

  • You can also assign a string directly to the variable name using a statement such as CString myString = “This is a text string.”;

Microsoft Visual C++ .NET Chapter 10


Dialog based applications l.jpg
Dialog-Based Applications

  • A dialog box is a window that is used to display information or to gather information from users

  • The message box you have seen in this chapter is an example of a dialog box

  • Figure 10-23 shows the Open dialog box that is shared by many Windows applications

Microsoft Visual C++ .NET Chapter 10


Dialog based applications36 l.jpg
Dialog-Based Applications

  • All dialog boxes—whether standard or custom—are created using two components: a dialog resource and a dialog class derived from the CDialog class

  • The dialog resource represents the visual aspect of the dialog box, and the dialog class provides programmatic access to the dialog box

  • Applications that use a dialog box as their primary interface window are called dialog-based applications

Microsoft Visual C++ .NET Chapter 10


Dialog based applications37 l.jpg
Dialog-Based Applications

  • In order for a class derived from CDialog to know which dialog box resource it is associated with, you must call a parameterized constructor for the CDialog base class and pass to it the enum variable that represents the dialog box resource ID

  • An enumerated, or enum, type declaration allows you to create your own data type to which you can assign only a series of predefined constant integer values

Microsoft Visual C++ .NET Chapter 10


Dialog based applications38 l.jpg
Dialog-Based Applications

  • You declare an enum type declaration using the syntax enum type_name (value1, value2, value3, …);

  • The values between the braces are known as enumerators and are used for symbolically representing a value

  • Each enumerator receives an integer value, starting with 0 for the first enumerator

Microsoft Visual C++ .NET Chapter 10


Modal and modeless dialog boxes l.jpg
Modal and Modeless Dialog Boxes

  • Modal dialog boxes require users to close or cancel the dialog box before they can continue working with an application

  • The message boxes you have worked with are examples of modal dialog boxes

  • Once a modal message box appears on your screen, you cannot access any other window in an application until you close the message box

Microsoft Visual C++ .NET Chapter 10


Modal and modeless dialog boxes40 l.jpg
Modal and Modeless Dialog Boxes

  • Figure 10-25 shows the modal Font dialog box for WordPad, a simple word-processing program supplied with Windows operating systems

Microsoft Visual C++ .NET Chapter 10


Modal and modeless dialog boxes41 l.jpg
Modal and Modeless Dialog Boxes

  • In comparison to a modal dialog box, modeless dialog boxes do not need to be closed before you return to another window in the application

  • Modeless dialog boxes function more like frame windows and other types of primary application windows

  • Modeless dialog boxes require quite a bit more work than modal dialog boxes

Microsoft Visual C++ .NET Chapter 10


Modal and modeless dialog boxes42 l.jpg
Modal and Modeless Dialog Boxes

  • Much of the behind-the-scenes work, such as closing and destroying the dialog window, is handled automatically with modal dialog boxes

  • With modeless dialog boxes, however, especially in dialog-based applications, you need to override several inherited member functions

  • Overriding the inherited member functions allows you to correctly close and destroy the dialog window

Microsoft Visual C++ .NET Chapter 10


Displaying modal dialog boxes l.jpg
Displaying Modal Dialog Boxes

  • You display a modal dialog box from an application’s InitInstance() function, the same way you display a frame window

  • You instantiate an object of the dialog class and use the inherited DoModal() function to display the modal dialog box

  • By default, if a user clicks a button containing a resource ID of IDOK or IDCANCEL, the dialog box closes

Microsoft Visual C++ .NET Chapter 10


Displaying modal dialog boxes44 l.jpg
Displaying Modal Dialog Boxes

  • The IDOK resource ID represents the OK button, and the IDCANCEL resource ID represents the Cancel button

  • The DoModal() function returns an integer value representing the resource ID that caused the dialog box to close

  • You use these resource IDs in an if statement to take the appropriate action, depending on whether the user pressed the OK button or the Cancel button

Microsoft Visual C++ .NET Chapter 10


Working with controls l.jpg
Working with Controls

  • Dialog boxes typically contain groups of controls through which a user interacts with an application

  • Controls are user interface items such check boxes, command buttons, text boxes, and other objects

  • You add controls to a dialog box by using the Controls toolbar in the Dialog Editor

Microsoft Visual C++ .NET Chapter 10


Working with controls46 l.jpg
Working with Controls

  • You can add the following three types of controls to MFC programs:

    • Windows common controls

    • MFC controls

    • ActiveX controls

  • Windows common controls are the standard controls, such as edit boxes, buttons, check boxes, and so on, you see in common dialog boxes

Microsoft Visual C++ .NET Chapter 10


Working with controls47 l.jpg
Working with Controls

  • MFC controls are provided by MFC and are not part of the Widows operating system

  • There are three MFC controls: the Bitmap Button control, the Checklist Box control, and the Drag List Box control

  • ActiveX is a technology that allows programming objects to be easily reused with any programming language that supports Microsoft’s Component Object Model

  • The Component Object Model, or COM, is an architecture for cross-platform development of client/server applications

Microsoft Visual C++ .NET Chapter 10


Working with controls48 l.jpg
Working with Controls

  • ActiveX controls are objects that are placed in Web pages or inside programs created with COM-enabled programming languages

  • ActiveX controls are very popular in Windows programming; you can literally find thousands of types of ActiveX controls in various places on the Web

  • Figure 10-29 in the text lists the Windows common controls, along with a description of each control and its associated MFC class

Microsoft Visual C++ .NET Chapter 10


Working with controls49 l.jpg
Working with Controls

  • You do not usually need to derive classes for individual controls placed on a dialog box

  • More complex types of controls must be controlled using an associated MFC class

  • To add controls to the MFC Calculator program’s dialog resource, perform the steps listed on pages 552 and 553 of the textbook

Microsoft Visual C++ .NET Chapter 10


Dialog data exchange l.jpg
Dialog Data Exchange

  • To set and retrieve control values in an MFC application, you can use the same SetWindowText() and GetWindowText() Windows API functions that you learned about in Chapter 8

  • MFC provides a special mechanism, however, called dialog data exchange, or DDX, to handle the exchange of values between controls and variables

  • You do not need to call the SetWindowText() and GetWindowText() functions in an MFC program because DDX handles the exchange of information for you

Microsoft Visual C++ .NET Chapter 10


Dialog data exchange51 l.jpg
Dialog Data Exchange

  • A related mechanism called dialog data validation, or DDV, assists in the validation of data as it is exchanged between controls and variables

  • Before you can use DDX or DDV, you must first override the DoData Exchange() function in the class derived from CDialog that is associated with your dialog resource ID

  • Figure 10-32 in the text shows an example of the Add Member Variable Wizard dialog box with the Control variable check box selected

Microsoft Visual C++ .NET Chapter 10


Dialog data exchange52 l.jpg
Dialog Data Exchange

  • The UpdateDate() function either initializes dialog box controls using associated data members, or it copies the current control values back into the associated data members

  • The UpdateData() function calls the DoDataExchange() function when it is passed a value of FALSE by the OnInitDialog() function

  • Add a DDX data member to the MFC Calculator program using the procedures shown on page 556 of the textbook

Microsoft Visual C++ .NET Chapter 10


Message maps l.jpg
Message Maps

  • A message map associates messages with message handler functions

  • All windows derived from CCmdTarget can include message maps

  • Because CWnd derives from CCmdTarget, any of the child class windows of CWnd can include their own message maps

  • You add a message map to a window by first adding the DECLARE_MESSAGE_MAP() macro to the class interface file

Microsoft Visual C++ .NET Chapter 10


Message maps54 l.jpg
Message Maps

  • You must add a message map block to the class implementation file starting with the BEGIN_MESSAGE_MAP() macro and ending with the END_MESSAGE_MAP() macro

  • One of the more common message macros you will use is the ON_COMMAND macro, which represents the events that are raised when a user selects a menu option or presses a shortcut key

  • The ON_COMMAND macro takes two parameters: a resource ID and the name of a message handler function

Microsoft Visual C++ .NET Chapter 10


Message maps55 l.jpg
Message Maps

  • To give the MFC Calculator program its functionality, you will use the BN_CLICKED macro, which is raised for the BN_CLICKED event

  • The Event Handler Wizard provides an automated way of associating a dialog control with an event

  • You use the Event Handler Wizard by highlighting a dialog control and then by selecting Add Event Handler from the Menu submenu on the Edit menu

  • Figure 10-34 in the text shows an example of the Event Handler Wizard dialog box

Microsoft Visual C++ .NET Chapter 10


Building an application wizard framework with the mfc application wizard l.jpg
Building an Application Wizard Framework with the MFC Application Wizard

  • You have now examined several of the most important pieces of an MFC framework

  • Although there are other important aspects of the MFC framework that you still need to explore, at this point you should be able to understand the parts of a simple dialog-based application created with the MFC Application Wizard

  • After you create the dialog-based application using the MFC Application Wizard, you still might not recognize much of what you see in the MFC framework

Microsoft Visual C++ .NET Chapter 10


Building an application wizard framework with the mfc application wizard57 l.jpg
Building an Application Wizard Framework with the MFC Application Wizard

  • Much of the MFC framework code is automatically created when you run the MFC Class Wizard, so you do not need to worry about it

  • One important aspect of MFC programs that you will not find in the MFC Class Wizard generated program is AfxWin.h include files

  • To create a dialog-based application using the MFC Application Wizard use the procedures illustrated on pages 566 and 567 of the textbook

Microsoft Visual C++ .NET Chapter 10


Summary l.jpg
Summary

  • The Microsoft Foundation Classes, or MFC, is a class library that helps programmer create Windows-based applications

  • A hook is a location in a program where a programmer can insert code that enhances functionality

  • The CWinApp class, also known as the application class, is responsible for initializing, starting, running, and stopping an MFC windows application

Microsoft Visual C++ .NET Chapter 10


Summary59 l.jpg
Summary

  • The CWnd class encapsulates the various Windows API functions, data types, and other code used for creating and instantiating windows

  • The MFC classes that derive from the CWnd class are used for creating the different types of windows that are visible to the user

  • A resource is a graphical user interface element or type of stored information that is used by a Windows application

Microsoft Visual C++ .NET Chapter 10


Summary60 l.jpg
Summary

  • Applications that use a dialog box as their primary interface window are called dialog-based applications

  • MFC provides a special mechanism called dialog data exchange, or DDX, to handle the exchange of values between controls and variables

  • Much of the MFC framework code is automatically created when you run the MFC Class Wizard

Microsoft Visual C++ .NET Chapter 10


ad