In these days of complex programs, reusability has become more than a buzzword. It's become a survival technique for programmers who find themselves with the awesome task of creating hundreds of thousands of lines of working source code in a minimum amount of time. Visual C++ is packed with ways to let you reuse the work of programmers who have gone before you, such as AppWizard, ClassWizard, and of course the Microsoft Foundation Classes. The tools discussed in this chapter enable you to contribute code to the future, ready to be reused quickly and easily by some future coworker--or better yet, by you.
If you have a job to do, it's easy to see how reusing someone else's code, dialog boxes, or design simplifies your work and lets you finish faster. As long as you can trust the provider of the material you reuse, the more you can reuse, the better. As a result, there's a market for reusable bits and pieces of programs.
In fact, there are two markets: one formal one, with vendors selling project parts such as controls or templates, and another informal one within many large companies, with departments developing reusable parts for brownie points or bragging rights, or other intangibles. Some companies even have a reuse budget to which you can charge the time you spend making parts of your project reusable, or they award reuse credits if someone else in the company reuses one of your parts. If yours doesn't, maybe it should: Reuse can save as much as 60% of your software budget, but only if someone is noble or charitable enough to develop with reuse in mind or if company policy inspires everyone to develop with reuse in mind.
Most newcomers to reuse think only of reusing code, but there are other parts of a project that can save you far more time than you can save with code reuse only. These include the following:
Component Gallery is one way that Developer Studio helps support reuse. Component Gallery gives you instant access to everything from reusable classes and OLE controls to wizards. You can even create your own components and add them to Component Gallery. In fact, in its default installation, Developer Studio automatically adds a category to Component Gallery for new AppWizard applications that you create.
Suppose you have a dialog box that you use frequently in projects. You can create this dialog box once, add it to Component Gallery, and then merge it into new projects whenever you need it. To see how this works, follow these steps:
FIG. 25.1 Build a dialog box to add to Component Gallery.
Although nothing appears to happen, the class CNameDlg and the associated resource have been added to the Gallery. Minimize Developer Studio and browse your hard drive, starting at My Computer, until you display C:\Program Files\Microsoft VisualStudio\Common\ MSDev98\Gallery (if you installed Visual C++ in another directory, look in that directory for the MSDev98 folder and continue down from there). As you can see in Figure 25.2, there is now an App1 folder in the Gallery.
FIG. 25.2 The Gallery uses your project name as the folder name when you add a class.
Double-click the App1 folder and you'll see it contains one file, Name Dlg.ogx, as shown in Figure 25.3. The .ogx extension signifies a Gallery component.
FIG. 25.3 The filename for your Gallery component is based on the classname.
Now that you've added the resource and associated class to the Gallery, a logical next step is to make another project that will use them. Create a MFC AppWizard (exe) application, called App2, with AppWizard. Again, click Finish on Step 1 to accept all the defaults and then OK to create the project.
Click the ClassView tab and expand the App2 classes. There are six: CAboutDlg, CApp2App, CApp2Doc, CApp2View, CChildFrame, and CMainFrame.
Choose Project, Add To Project, and Components and Controls. The Gallery dialog box, shown in Figure 25.4, appears.
FIG. 25.4 Gallery components are arranged in folders.
Double-click App1 and you'll see Name Dlg.ogx again. Double-click it. When prompted, confirm that you want to insert this component in your project. Click Close to close the Gallery.
Look at ClassView again. CNameDlg has been added. Check FileView and you'll see that NameDlg.cpp and NameDlg.h have been added to the project. Switch to ResourceView to confirm that the dialog box IDD_NAMEDLG has been added. You can use this resource in App2 in just the way you used it in App1.
You can use Component Gallery to manage many other component types, including those that you might get from a friend or buy from a third-party supplier. Component Gallery can add, delete, import, and edit components in a variety of ways, depending on the type of component with which you're working. Take some time to experiment with Component Gallery, and you'll soon see how easy it is to use.
Figure 25.5 shows the contents of the Registered ActiveX Controls folder, reached by choosing Project, Add to Project, Components and Controls. Both the ATL and MFC versions of the Dieroll control are here: DieRoll Class was built in Chapter 21, "The Active Template Library," and Dieroll Control was built in Chapter 17, "Building an ActiveX Control." Before this shot was taken, DBGrid Control was highlighted and the More Info button was clicked. Components can be bundled with a Help file that is reached from the More Info button.
AppWizard is a sensational tool for starting projects effortlessly. However, because of its general nature, AppWizard makes many assumptions about the way you want a new project created. Sometimes you may need a special type of AppWizard project that isn't supported by the default AppWizard. If this special project is a one-time deal, you'll probably just create the project by hand. However, if you need to use this custom project type again and again, you might want to consider creating a custom AppWizard.
FIG. 25.5 All ActiveX controls are available through the Gallery.
You can create a custom AppWizard in three ways: using the existing AppWizard steps as a starting point, using an existing project as a starting point, or starting completely from scratch. However, no matter what method you choose, creating a custom AppWizard can be a complicated task, requiring that you understand and be able to write script files by using the macros and commands that Visual C++ provides for this purpose.
The following tackles the very simplest case first, creating an AppWizard to reproduce an existing project with a different name. Follow these steps:
FIG. 25.6 Customize your About box.
FIG. 25.7 Create a custom AppWizard.
FIG. 25.8 Base your wizard on an existing project.
You are now working on the OrigWiz project, and in many cases you would add code at this point. Because this is an example, just build the project immediately.
To use your custom AppWizard, choose File, New again and click the Projects tab. As shown in Figure 25.10, OrigWizard has been added to the list of choices on the left. Select it and enter App3 for the name of the project. Click OK.
NOTE: When you compile the custom AppWizard, Developer Studio creates the final files and stores them in your C:\Program Files\Microsoft Visual Studio\Common\MSDev98\ Template directory. The next time you choose to start a new project workspace, your custom AppWizard will be listed in the project types. To remove the custom AppWizard, delete the wizard's .awx and .pdb files from your C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Template directory.
FIG. 25.9 Your custom AppWizard creates copies of the Original project with different names.
FIG. 25.10 Your custom AppWizard has been added to the list of AppWizards.
Figure 25.11 shows one of the tasks that you normally complete before you build the AppWizard: generating the text for the New Project Information dialog box. Click OK.
Look at the classnames and the code--App3 looks like any of the projects created in this chapter that accept all the AppWizard defaults, but you didn't have to go through any dialog steps. Switch to ResourceView and edit IDD_ABOUTBOX. As Figure 25.12 shows, it contains the extra text (based on Original 1.0) that you added, but the application name on the top line of the box has been correctly changed to App3. This is one smart wizard.
When you build a wizard from an existing project, all the classes, resources, and code that you added will be incorporated in the new projects you generate with the wizard. It's a great time-saver.
FIG. 25.11 You have to write the text for the New Project Information dialog box.
FIG. 25.12 AppWizard copied your custom About box to the new project.
You can also build custom AppWizards that present dialog boxes for you to fill out. Before you do that, you should be comfortable writing wizards that are not AppWizards, like the ones discussed in Chapter 12, "Property Pages and Sheets." You should also have generated lots of different types of applications so that you have a feel for the sort of work AppWizard does. When you're ready, check the section in the online help titled "Creating Custom AppWizards."
This whole book demonstrates the value of using other people's designs, classes, code, controls, dialog boxes, and other project parts. This chapter shows two simple ways to arrange for other people (or you, in the future) to reuse your code, which benefits your customers or employer by saving significant development time. Your job will be more enjoyable when repetitive tasks, such as building a dialog box and associating it with a class, are taken care of, freeing you to do the fun stuff.
© Copyright, Macmillan Computer Publishing. All rights reserved.