第4章
Visual C++.NET Wizards
Visual C++.NET向导
Developing for the Microsoft platform involves a lot of boilerplate coding. Back in the early days of Windows development, most developers started a Windows-based Project armed with only a copy of Charles Petzold’s Programming Windows and the Windows Software Development Kit(SDK). Even the Windows SDK documentation recommended the editor inheritance method of application development.
To understand the fundamental underpinnings of any technology, you have to write all the code for an application. But there comes a time when writing the same boilerplate again and again becomes merely a drill and a waste of time. To address this issue, the Microsoft Visual Studio.NET environment provides a set of code generators to start you off on all types of projects. The available project templates appear in the New Project window when you choose New, Project from the File menu. You select the project template you want, run through the dialog boxes to configure the project, and click Finish. Voila—you’ve got a working application.
But that’s not the end of the story. The wizard technology is extensible you can write your own wizards. This chapter gives you a rundown of Visual Studio.NET ’s wizards and explains how to write your own.
在微软平台的发展中有许多样板代码,早期Windows开发中,许多开发者都学习Charles Petzold的Windows程序设计和Windows开发工具包的代码来开始自己的Windows项目.每个Windows SDK文件都推荐开发者用继承的方法应用到应用程序的开发中.
要理解每个技术的基础原理,你要在应用程序中写全部的代码.但是有时当你重复在三的写相同的样板代码是就变成了在练习和浪费时间.为解决这个问题,微软的VS.NET环境提供一个代码产生器的设置让你远离相同类型的项目.在文件菜单的项目中,选择新建,可用的项目模板就出现在新项目窗口中.你根据你的需要来选择项目模板,快速处理配置对话框里的项目配置,点完成.那就是---你得到一个可以工作的应用程序.
但是故事并没有就此结束.这个向导技术可以根据你的需要自己定制.这章将告诉你VS.NET的向导的概要和说明怎么建立自己的向导.
Wizard Types
Visual Studio.NET supports two types of wizards: those with a user interface and those without. Depending on the complexity of your wizard, you might want to include a user interface. Most of the wizards you’ll see in this book include one. For example, the MFC Application Wizard includes several pages that provide such options as type of document interface(SDI or MDI), whether to support printing and print preview, and whether to use ActiveX controls. For simple application types. You might not need a user interface.
Wizards without user interfaces simply take a project name you supply and generate project files based on templates you provide. Wizards with user interfaces tend to be a little more involved and can include several pages of application options.
In fact, the source code for all the Visual C++.NET wizards is available. You can find the Wizard source code files in the \Program Files\Microsoft Visual Studio.NET\VC7\VCWizards directory.
向导类型
VS.NET支持两种类型的向导:有用户界面的和没有用户界面的.依靠向导的复杂性,你可以建立有用户界面的.很多的向导在本书中你都能看到.例如,MFC应用程序向导包含几个页面给文档界面(SDI或MDI)提供这样的选项,是否支持打印和打印预览,是否使用ActiveX控件.简单的应用程序类型,你可以不需要用户界面.
没有界面的向导只需简单到你提供项目名就可以产生基于你提供的模扳的项目文件.有用户界面的向导要麻烦一些,它包括几个页面为应用程序提供选项.
事实上,VC++.NET向导的源代码是可以利用的.你能在\Program File\Microsoft Visual Studio.NET\VC7\VCWizards目录下找到源代码文件.
How Wizards Work
Before we create a wizard, let’s look at how wizards work. We’ll look at the three main parts of a wizard: the original boilerplate code, the user interface, and the code it generates.
The general idea behind a code generator is to create a basic project for you to relieve you from having to type all the boilerplate code. That basically means a bare-bones application or library that works and compiles. However, you want the code to reflect the nature of the project. For example, if you’re writing a payroll application, you want the classes in the application to have names like CPayrollDoc, CPayrollView, and CPAYROLLfRAME. It’s the wizard’s job to substitute the plain vanilla names of the basic application with the names the developer types in.
The wizard is also responsible for adding or leaving out certain parts of code, depending on the developer’s selections. For example, if you select the About dialog box from a list of options, the wizard will add the correct code for the dialog box to the finished application.
The wizard presents these choices through a user interface. The heart of the wizard’s interface is an HTML control named IVCWIzCtrlUI. The Visual Studio.NET wizards use HTML to drive the user interface. When you execute the wizard, the IVCWizCtrlUI interface looks for the list of files representing the user interface and displays those pages within the wizard. The wizard is responsible for managing navigation through each of the pages as well as generating the code when the developer clicks the Finish button.
A wizard can contain any number of pages, each driven by a separate HTML file. The wizard provides navigation functionality through the Next and Back buttons(or any other format you specify). The HTML files that implement the wizard interface contain the SYMBOL tag, which identifies the default for developer-defined options.
向导是怎么工作的
在我们建立向导之前,让我们来看看向导是怎么工作的.我们将考虑向导的三个主要部分:最初的样板代码,用户界面和代码产生.
一般认为代码产生器建立基本的项目帮你减轻所有类型的样本代码.基本意味着一个空壳的应用程序或库.然而,你想要代码体现的是现实的项目.例如:你想写个薪水应用程序,你希望应用程序的类都类似与CpayrollDoc,CPayrollView和CpayrollFrame的名字.向导的工作就是用开发者输入的名字来替换应用程序的名字.
依靠开发者选择的不同,向导同样负责增加或去除某几部分代码.例如,你从列表选项中选择了关于对话框,向导将为关于对话框增加恰当的代码来完成应用程序.
向导现在通过用户界面来选择.这个向导界面的核心是一个名为IVCWizCtrlUI的HTML控件.VS.NET使用HTML来驱动用户界面.当你执行向导是, IVCWizCtrlUI界面寻找表现用户界面的列表文件并显示在向导的页面上.当开发者单击完成按钮时向导负责管理并为每个页面产生恰当的代码.
向导包含很多单独的HTML页.向导通过Next和Back按钮来提供导航功能(或你定义的任意种方式).实现向导界面的HTML文件包含着SYMBOL标志,那些是开发者定义选项的缺省定义.
The wizard maintains a symbol table during the lifetime of its execution.
The symbol table is just a dictionary lookup mechanism for making substitutions. The symbols declared in the HTML file are written into the symbol table when the user clicks Finish. For example, examine the following HTML in a wizard user interface:
<SYMBOL NAME=’SOURCE_FILE’ VALUE=’MySource.cpp’ TYPE=text></SYMBOL>
In the wizard user interface, the text box represents an input box for the user to type into. The text box is identified using the symbol SOURCE_FILE. This is the key the wizard will look for when it makes substitutions for source files. We’ll look at how that works in just a minute. Basically, each HTML file used by the wizard is responsible for recording user selections to the symbols table.
Logic within the wizards is usually implemented using Jscript. If you need to provide customized behavior from within the wizard, you can use Jscript functions to access the Visual C++ Wizard Model. These functions are in the HTML page section headed <SCRIPT LANGUAGE=’JSCRIPT’>.
Note For more information about the Visual C++ Wizard Model and other object models that make up the Visual C++ Extensibility Object Model, refer to the MSDN Library.
向导终身都在维护符号表.这个符号表仅仅是为安排替换的一种字典查找机制,当你单击完成时这些符号是定义在HTML文件中的写入符号表中的.例如:剖析下面的向导用户界面的HTML:
<SYMBOL NAME=’SOURCE_FILE’ VALUE=’MySource.cpp’ TYPE=text></SYMBOL>
在向导用户界面中,这个文本框是一个典型的用户输入框.这个文本框是用符号SOURCE_FILE定义的.当向导构造替换是将在源文件中查找这个关键字.我们将看到那一瞬间是怎么工作的.基本上,每个HTML文件都是向导记录用户选择给符号表的责任.
在向导的内部逻辑常常是用JSCRIPT来实现的.如果你需要在向导内部提供自定义的能力,你可以用JSCRIPT函数来访问VC++向导模块.这个函数放在HTML页的头部分<SCRIPT LANGUAGE=’JSCRIPT’>.
注意 关于VC++向导模型和补充的VC++扩展性对象模型的其它对象模型的更多信息.请查阅MSDN库
Creating a Wizard
The first step in creating a wizard is to write and debug a boilerplate application. Once you’ve done that, you can use Visual Studio.NET to generate a blank wizard for you. Visual Studio.NET includes a wizard named the Custom Wizard for creating wizards. This wizard generates all the files necessary to implement a wizard.
To create a wizard, choose New, Project from the File menu, select Visual C++ Projects, and then select the Custom Wizard template. Type a name for the wizard in the Project Name text box. The Custom Wizard includes only two pages: an overview page and the Application Settings page. The Application Settings page lets you specify a wizard-friendly name, whether to include a user interface, and the number of pages in the wizard. The files created by the Custom Wizard are listed in Table 4-1.
建立向导:
建立向导的第一步是书写和调试样本应用程序.一次完成之后,就能用VS.NET为你产生一个空白的向导.VS.NET在建立向导时也包括定制向导.这是向导在执行一个向导时产生的必须文件.
要建立向导,从文件菜单中选择新建,选择项目,选择VC++项目并选择自定义向导模板.在项目名称文本框中输入向导名字.这个向导包括两个页面:一个概述页和应用程序设置页.应用程序设置页让你指定向导友好的名字.是否包含用户界面和向导页数.表4-1列出了自定义向导时的文件.
Table 4-1 Files Generated by the Custom Wizard
Files Description
Project.vsz A text file that identifies the wizard engine and
Provides context and optional custom parameters.
Project.vsdir A text file that provides a routing service between the Visual Studio shell and the items in the wizard project.
HTML files(optional) Files for wizards that implement a user interface. The wizard user interface is implemented as HTML. Wizards without a user interface do not contain HTML files.
The Default.htm file specifies the user interface features. Wizard with more than one page specified in the Application Settings page of the Custom Wizard include additional files: these files are named Page_PageNum.htm.
Script files Wizard logic code is executed as script. A wizard includes a Jscript file named Default.js for each project. A wizard also includes Common.js. These files contain Jscript functions that access the Visual C++ Wizard, Code, Project, and Resource Editor Models to customize a wizard. You can customize and add functions in the wizard project’s Default.js file.
Template files A collection of text files in the Templates directory that contain directives. These files are parsed and inserted into the symbol table based on the user’s selections. The template text files are rendered according to the user input and added to the project. The appropriate information is obtained by directly accessing the wizard control’s symbol table.
Templates.inf A text file that lists all templates associated with the project.
Default.vcproj An XML file that contains the information on the project type.
Sample.txt A template file that shows how your wizard directives are used.
ReadMe.txt A template file that contains a summary of each file created by the Custom Wizard.
Images file(optional) A file of images, such as icons, GIFs, BMPs, and other HTML-supported image formats, to enhance your wizard’s user interface. Of course, a wizard that has no user interface does not need images.
Styles.css(optional) A file that defines the styles for the user interface. Again, if your wizard has no user interface, the Custom Wizard does not create a CSS file.
Common.js Common Jscript functionality used by all wizards. This file isn’t actually generated by the Custom Wizard—it’s included in the source code that’s generated.
表4-1 自定义向导产生的文件
文件 说明
Project.vsz 该文本文件描述的是向导引擎和上下文关系和可选自定义参数
Project.vsdir 该文本文件给VS外壳和向导项目成员间提供路由服务
HTML文件(可选) 文件是向导的用户界面实现.向导的用户界面都是可执行的HTML.没有用户界面的向导自然就没有HTML文件.
Default.htm文件指定了用户界面的特征.自定义向导包含一个或多个应用程序设置页文件:这些文件被命名为Page_PageNum.htm格式.
脚本文件 向导的逻辑代码是可执行的脚本.每个向导项目都包含一个用Jscript脚本写的名为Defaule.js的文件.向导同样也包含一个Common.js的文件,该文件包含的Jscript的函数可以访问VC++的向导,代码,项目和资源编辑模块.在Default.js中你可以定制和增加向导项目的功能.
模板文件 该文本文件集合在模板目录里包含了模板指令.这些文件解析和插入基于用户选择的符号表.这些文件将依照用户的输入增加到项目中.适当信息的获得是直接访问向导控制的符号表.
Templates.inf 该文本文件列出了项目相关的所有模板.
Default.vcproj 这是个XML文件包含了项目的类型信息.
Sample.txt 此模板显示如何使用向导指令
ReadMe.txt 此模板包含向导建立时每个文件摘要.
图象文件(可选) 图象文件,例如Icons,GIFs,BMPs和其他HTML支持的图象格式,来提高你的向导的用户界面.当然,没有用户界面的向导是不需要图象的.
Styles.css(可选) 该文件定义了你用户界面的风格.此外,如果你的向导没有用户界面,自定义向导就不用建立CSS文件.
Common.js 所有向导都使用的公共的Jscript函数.这个文件在自定义向导中事实上不会产生的—它是包含在源代码里的.