The Phone Book program developed in the next three chapters is a simple name and phone number database. It stores information about people: first name, last name, and phone number. Using the Windows interface, you can create a new database, fill it with information, and save it to a disk file. You can also open an existing database from a file and add names, delete names, and edit information. You can find all entries with the same last name and display the list of found entries. And you can print your database files.
The Phone Book program displays a window with a menu bar. The menu bar contains three menus, one for file operations, one for database operations, and one for Help.
If you create a new, empty database, the window title changes from “Phone Book” to “Phone Book–Untitled” and you can then add entries with the Add command in the Person menu. When you choose the Save or Save As command from the File menu, a dialog box prompts you to enter a filename, the database is serialized to the file, and the new filename becomes the new window title.
If you open an existing database, you are first prompted to save the existing one if it has unsaved changes. Then a standard Windows dialog box prompts you for a filename, the persistent data stored in the file is deserialized into an automatically created CPersonList object, and the new filename replaces the old one in the title bar. The data is displayed in the window, one line per person. If there's more data than the window can display, scrollbars are added to the window, and you can use them to scroll to data that isn't currently displayed. You can also scroll with the RIGHT ARROW, LEFT ARROW, PAGE UP, PAGE DOWN, HOME, and END keys.
To delete or edit a person's information, you must first select the person's line of information in the window. You can choose a person in the database by selecting the person's line of information in the display, either with the mouse or with the UP ARROW and DOWN ARROW keys.
If you add, delete, or edit the information in a database, the database is flagged internally to indicate that it has unsaved changes. When you add, delete, or edit a person's information, the display changes to reflect the new information.
If you search the database for a name, a list of matching list elements is displayed in the window, replacing the display of the full database. Changes to this list are reflected in the main list. To abandon the list of search results and display the full database again, you choose the Find All command in the Person menu.
If you choose the Print command in the File menu, a standard Windows print dialog box is displayed so you can select printing options.
If your file is unsaved and you choose the Save or Save As commands in the File menu, a standard Windows Save dialog box is displayed so you can name the file and select a directory for it. If the file has already been saved, you can choose the Save command to save recent changes.
Figure 4.1 shows the screen as it appears with an open database on display.
This example program does considerably more than Hello and provides you with a larger model on which to pattern your own Windows programs written with the Microsoft Foundation Class Library.
To view the complete code for Phone Book, see Listings 1 and 2 in this chapter and Listings 1 and 2 in Chapter 5. The listings in the current chapter give the code in files DATABASE.H and DATABASE.CPP. The listings in Chapter 5 give the code in files VIEW.H and VIEW.CPP. To review the code for the Data Model, see Windows Listings 1 and 2 in Chapter 2.
The code shown in Listings 1 and 2 is available on the distribution disks in files DATABASE.H and DATABASE.CPP.
Microsoft Foundation Classes Used in This Chapter
This chapter and the two following chapters use the classes employed in Chapters 2 and 3. Many of these classes are used in the same way, since Phone Book is built from the foundations of Hello and the Data Model program. But one class in particular, CModalDialog, is used more extensively in Chapter 5, and the CMenu class is also employed. CMenu provides access to the menus in the menu bar and is used for updating menus to suit the context. Menu commands unavailable in the current context are dimmed.
The sections that follow take you through the components of Phone Book, explaining how to write them, what they consist of, and how they work.