📄 ch25.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 25</TITLE>
<LINK REL="Next" HREF="ch26.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch26.htm">
<LINK REL="Previous" HREF="ch24.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch24.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><B>Chapter 25</B></H2>
<H2><B>Achieving Reuse with the
Gallery and Your Own AppWizards</B></H2>
<hr>
<P>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.</P>
<ul>
<li> <B>Reuse and Why you want it</B></P>
<P> The fastest way to
write working, tested code is to already have it.</P>
<li> <B>How to </B><B>A</B><B>dd </B><B>C</B><B>omponents to Component Gallery</B></P>
<P> Reusable components can be anything from a dialog box to an OLE control. Component Gallery organizes
components so that you can add them to your projects with a couple of mouse clicks.</P>
<li> <B>About </B><B>C</B><B>reating </B><B>C</B><B>ustom AppWizards</B></P>
<P> Generate your own kinds of projects, customized for the way your team (even if it's a
team of one) likes to work.</P>
</ul>
<H3><B>Reviewing the Benefits of Writing Reusable Code</B></H3>
<P>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.</P>
<P>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.</P>
<P>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:</P>
<ul>
<li> <B>Design</B>. The Document/View paradigm, first discussed in <A HREF="index05.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index05.htm" target="text">Chapter 5</A>,
"Documents and Views," is a classic example of a design decision that is reused in project after project.</P>
<li> <B>Interface Resources</B>. You can reuse controls, icons, menus, toolbars, or entire dialog boxes and reduce training time for
your users as well as development time for your programmers.</P>
<li> <B>Project Settings</B>. Whether it's an obscure linker setting or the perfect arrangement of toolbars, your working environment must be right for you, and getting it right is faster on
every project you do, because you reuse the decisions you made last time.</P>
<li> <B>Documentation</B>. As you read in <A HREF="index11.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index11.htm" target="text">Chapter 11</A>, "Help," help text for standard commands like <U>F</U>ile, <U>O</U>pen is
generated for you by AppWizard. You can reuse your own help text from project to project and save even more time.</P>
</ul>
<H3><B>Using Component Gallery</B></H3>
<P>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.</P>
<P><B>Adding a Component to the Gallery</B></P>
<P>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:</P>
<ol>
<li><P> 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.)</P>
<li><P> Add a new dialog box to the project by choosing <U>I</U>nsert, <U>R</U>esource and double-clicking Dialog.</P>
<li><P> Using the techniques first presented in <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter
2</A>, " Dialog Boxes and Controls," build the dialog-box resource shown in Figure 25.1, giving the dialog box the resource ID <font color="#008000">IDD_NAMEDLG</font>.</P>
</ol>
<A HREF="Zfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch25/Zfigs01.gif"><b>Fig. 25.1</b></A>
<P><I>Build a
dialog box to add to Component Gallery.</I></P>
<ol start=4>
<li><P> While the dialog box has focus, bring up ClassWizard and agree to create a new class. Call the new class <font color="#008000">CNameDlg</font>.</P>
<li><P> Close ClassWizard.</P>
<li><P>
Right-click on <font color="#008000">CNameDlg</font> in ClassView and choose Add To Gallery from the shortcut menu.</P>
</ol>
<P>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.</P>
<A HREF="Zfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch25/Zfigs02.gif"><b>Fig. 25.2</b></A>
<P><I>The Gallery uses your project name as the folder name when you add a </I><I>class.</I></P>
<P>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.</P>
<A HREF="Zfigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch25/Zfigs03.gif"><b>Fig. 25.3</b></A>
<P><I>The file name for your Gallery
component is based on the class name.</I></P>
<P><B>Using Gallery Components in Your Projects</B></P>
<P>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.</P>
<P>Click on the ClassView tab and expand the App2 classes. There are six: CAboutDlg, CApp2App, CApp2Doc,
CApp2View, CChildFrame and CMainFrame.</P>
<P>Choose <U>P</U>roject, <U>A</U>dd To Project, Com<U>p</U>onents and Controls. The Gallery dialog, shown in Figure 25.4, appears.</P>
<A HREF="Zfigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch25/Zfigs04.gif"><b>Fig. 25.4</b></A>
<P><I>Gallery components
are arranged in folders.</I></P>
<P>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.</P>
<P>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.</P>
<P><B>Exploring the Gallery</B></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -