1 / 60

Microsoft Foundation Classes

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

benjamin
Download Presentation

Microsoft Foundation Classes

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Microsoft Foundation Classes Microsoft Visual C++ .NET Chapter 10

  2. 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

  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 Microsoft Visual C++ .NET Chapter 10

  4. 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

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  7. 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

  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 Microsoft Visual C++ .NET Chapter 10

  9. Partial Listing of the MFC Class Hierarchy Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 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

  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 Microsoft Visual C++ .NET Chapter 10

  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 class’s InitInstance() function using an instantiated object of the window class Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  25. 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

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  28. Properties Window for a Button in the Dialog Box Editor Microsoft Visual C++ .NET Chapter 10

  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 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

  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 Microsoft Visual C++ .NET Chapter 10

  31. 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

  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 Microsoft Visual C++ .NET Chapter 10

  33. A Member Function in the Properties Window Microsoft Visual C++ .NET Chapter 10

  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.”; Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  36. 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

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  43. 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

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

  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 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

  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 Microsoft Visual C++ .NET Chapter 10

  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 program’s dialog resource, perform the steps listed on pages 552 and 553 of the textbook Microsoft Visual C++ .NET Chapter 10

  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 Microsoft Visual C++ .NET Chapter 10

More Related