Chapter 25

Achieving Reuse with the Gallery and Your Own AppWizards


In these days of complex programs, reusability has become more than just 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, like AppWizard, ClassWizard, and of course MFC, the Microsoft Foundation Classes. The tools discussed in this chapter let you be the one who contributes code to the future, ready to be reused quickly and easily by some future coworkeróor better yet, by you.

Reviewing the Benefits of Writing Reusable Code

If you have a job to do, it's easy to see how reusing someone else's code, dialog boxes, or design will make your work simpler and get you finished faster. As long as you can trust the provider of material you will reuse, the more you can reuse, the better. That means 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 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 percent 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:

Using Component Gallery

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.

Adding a Component to the Gallery

Suppose you have a dialog box that you use frequently in projects. You can create this dialog box just once, add it to Component Gallery, and then merge it into new projects whenever you need it. To see how this might work, follow these steps:

  1. Start a new AppWizard project workspace called App1. (Just click Finish on Step 1 to use all the default AppWizard settings, then click OK to create the project.)
  2. Add a new dialog box to the project by choosing Insert, Resource and double-clicking Dialog.
  3. Using the techniques first presented in Chapter 2, " Dialog Boxes and Controls," build the dialog-box resource shown in Figure 25.1, giving the dialog box the resource ID IDD_NAMEDLG.

Fig. 25.1 Build a dialog box to add to Component Gallery.

  1. While the dialog box has focus, bring up ClassWizard and agree to create a new class. Call the new class CNameDlg.

  2. Close ClassWizard.
  3. Right-click on CNameDlg in ClassView and choose Add To Gallery from the shortcut menu.

Although it appears that nothing has happened, 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 are displaying C:\Program Files\DevStudio\SharedIDE\Gallery (if you installed Visual C++ into another directory, look in that directory for the SharedIDE 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 file name for your Gallery component is based on the class name.

Using Gallery Components in Your Projects

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 another application with AppWizard called App2 and again click Finish on Step 1 to accept all the defaults, then OK to create the project.

Click on the ClassView tab and expand the App2 classes. There are six: CAboutDlg, CApp2App, CApp2Doc, CApp2View, CChildFrame and CMainFrame.

Choose Project, Add To Project, Components and Controls. The Gallery dialog, shown in Figure 25.4, appears.

Fig. 25.4 Gallery components are arranged in folders.

Double click App1 and you will see Name Dlg.ogx again. Double-click it. When prompted, confirm that you want to insert this component into your project. Click Close to close the Gallery.

Look at ClassView again. CNameDlg has been added. Check in FileView and you'll see that NameDlg.cpp and NameDlg.h have been added to the project. Switch to ResourceView to confirm thatthe dialog IDD_NAMEDLG has been added. You can use this resource in App2 in just the way you used it in App1.

Exploring the Gallery

You can use Component Gallery to manage many other types of components, 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 upon the type of component with which you're working. Take some time to experiment with Component Gallery, and you'll quickly see how easy it is to use.

Figure 25.5 shows the contents of the Registered ActiveX Controls folder. 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, Grid 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.

Fig. 25.5 All ActiveX controls are available through the Gallery.

Introducing Custom AppWizards

AppWizard is a sensational tool for getting projects started quickly and easily. 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'd probably just go ahead and 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.

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 provided by Visual C++ for this purpose.

The followingtackles the very simplest case first, creating an AppWizard that can reproduce an existing project with a different name. Follow these steps:

  1. Create a project in the usual way. Call it Original and click Finish on Step 1 to accept all the AppWizard defaults.
  2. Edit the About box to resemble Figure 25.6

Fig. 25.6 Customize your About box.

  1. Choose File, New, and click the Projects tab. Select Custom AppWizard and enter OrigWiz, as shown in Figure 25.7. Click OK.

Fig. 25.7 Create a Custom AppWizard

  1. The first of two custom AppWizard dialogs appears, as shown in Figure 25.8. Select An Existing Project to base your wizard on the project you created in steps 1 and 2. Do not edit the name of the wizard. Click Next.

Fig. 25.8 Base your wizard on an existing project.

  1. The second custom AppWizard dialog box appears, shown in Figure 25.9. Browse to the project file for the Original project. Click Finish.

  2. The New Project Information dialog, shown in Figure 25.10, confirms your choices. Click OK.

Fig. 25.9 Your Custom AppWizard will create copies of the Original project with different names.

You are now working on the OrigWiz project, and in many cases you would add code at this point. Since this is a sample, just build the project immediately.

To use your custom AppWizard, choose File, New again and click the Projects tab. As shown in Figure 25.11, 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.

Fig. 25.10 Your Custom AppWizard has been added to the list of AppWizards.

When you compile the custom AppWizard, Developer Studio creates the final files and stores them in your C:\Program Files\DevStudio\SharedIDE\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\DevStudio\SharedIDE\Template directory.

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

Fig. 25.11 You have to write the text for the New Project Information dialog.

Look at the class names 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.13 shows, it contains the extra text (Based on Original 1.0) you added, but the application name on the top line of the box has been correctly changed to App3. This is one smart wizard.

Fig. 25.12 Your custom About box was copied to the new project by your AppWizard.

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

You can also build custom AppWizards that present dialogs 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 and Wizards." 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 entitled "Creating Custom AppWizards."

From Here...

This whole book has shown you the value of using other people's designs, classes, code, controls, dialog boxes, and other project parts. This chapter has shown two simple ways that you can arrange for other people (or your future self) to reuse your code. This can bring real benefits to your customers or employer by saving significant development time. And it can make your job more fun, by taking care of the repetitive tasks like building a dialog and associating it with a class, freeing you to do the fun stuff.

For more information on related topics, please refer to the following chapters:


© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.