microsoft foundation classes
Download
Skip this Video
Download Presentation
Microsoft Foundation Classes

Loading in 2 Seconds...

play fullscreen
1 / 60

Chapter 10 - PowerPoint PPT Presentation


  • 291 Views
  • Uploaded 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
Microsoft Foundation Classes

Microsoft Visual C++ .NET Chapter 10

objectives
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
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
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
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
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
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
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
Partial Listing of the MFC Class Hierarchy

Microsoft Visual C++ .NET Chapter 10

mfc class hierarchy10
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 window29
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
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
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
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
A Member Function in the Properties Window

Microsoft Visual C++ .NET Chapter 10

the cstring class
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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