One of Microsoft's main goals in designing MFC was to create a C object ... After you create the dialog-based application using the MFC Application Wizard, ... – PowerPoint PPT presentation
How to build an application framework with the MFC Application Wizard
3 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
4 Microsoft Foundation Classes
One of Microsofts 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
5 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
6 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
7 MFC Class Library
The MFC class library consists of two major sections
The MFC class hierarchy
Global functions and macros
8 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
9 Partial Listing of the MFC Class Hierarchy 10 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
11 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
12 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
13 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
14 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
15 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
16 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
17 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
18 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 classs implementation file
Figure 10-10 in the text shows an example of a basic application class named CbasicApp
19 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
20 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 classs InitInstance() function using an instantiated object of the window class
21 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
22 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
23 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
24 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
25 Resource Editors
Resource editors allow you to quickly create and modify resources in a graphical environment
You can still edit a resource files 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
26 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
27 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
28 Properties Window for a Button in the Dialog Box Editor 29 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 objects properties by category
An Alphabetic button which displays all of an objects properties alphabetically
A Properties button which displays an objects properties
A Property Pages button which displays the projects Property Pages dialog box
30 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
31 Properties Window
In order to display a class members 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
32 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
33 A Member Function in the Properties Window 34 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.
35 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
36 Dialog-Based Applications
All dialog boxeswhether standard or customare 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
37 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
38 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
39 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
40 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
41 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
42 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
43 Displaying Modal Dialog Boxes
You display a modal dialog box from an applications 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
44 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
45 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
46 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
47 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 Microsofts Component Object Model
The Component Object Model or COM is an architecture for cross-platform development of client/server applications
48 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
49 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 programs dialog resource perform the steps listed on pages 552 and 553 of the textbook
50 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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
About PowerShow.com
PowerShow.com is a leading presentation/slideshow sharing website. Whether your application is business, how-to, education, medicine, school, church, sales, marketing, online training or just for fun, PowerShow.com is a great resource. And, best of all, most of its cool features are free and easy to use.
You can use PowerShow.com to find and download example online PowerPoint ppt presentations on just about any topic you can imagine so you can learn how to improve your own slides and presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!
For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!