当前位置:文档之家› Windows的编程模式的外文翻译

Windows的编程模式的外文翻译

Windows的编程模式的外文翻译
Windows的编程模式的外文翻译

附件1:外文资料翻译译文

Windows的编程模式

不管您使用什么样的开发工具,Windows程序设计同已经过时的面相批命令或面向事务的程序设计已经有了根本的不同。作为开始,您必须了解一些有关Windows 的基础知识,我们将参照大家数值的MS-DOS编程模式来进行介绍。尽管读者现在可能已不再编写纯粹的MS-DOS程序了,但相信读者对MS-DOS编程还是比较熟悉的。1.1.1消息处理

当用C来编写MS-DOS应用程序时,最起码要又一个mian函数。当用户运行改应用程序时,操作系统会自动条用main,从这一点上讲,您可以使用任何所需要的程序结构。如果程序需要得到用户的键盘输入,或者需要使用操作系统所提供的功能,那么它就可以调用适当的函数,如getchar,或者使用基于字符的窗口库。

当Windows操作系统运行程序时,它首先调用程序中的WinMain函数。因此,在Windows应用程序中一定要有WinMain函数,该函数一般用来完成某些特殊的任务,其中最重要的任务就是要创建该应用程序的“主窗口”。“主窗口”中必须包含用来处理Windows所发送的消息的代码。基于Windows的程序和机遇MS-DOS的程序之前的一个最根本的差别,就在于MS-DOS程序师通过调用操作系统的功能来获得用户输入的,而Windows程序则是通过操作系统发送的消息来处理用户输入的。

说明:许多Windows开发环境,包括使用Microsoft MFC库 4.21版的Microsoft Visual C++5.0,都通过隐藏WinMain函数及构造消息-控制机制来简化编程。虽然使用MFC库编程不需要WinMain函数,但是弄清楚操作系统和程序之间的这种联系时最基本的要求。

许多Windows消息都经过了严格的定义,并且适用于所有的程序。例如,当窗口被创建时系统就会发送WM_CREATE消息,当用户按下鼠标的左键时就会发送WM_LBUT-TONDOWN消息,当用户敲了一个字符键时系统就会发送WM_CHAR消息,而当用户关闭窗口时系统又会发送WM_CLOSE消息。所有的消息都有32位的参数,可以保存如光标位置、键盘码等这样的信息。当用户进行菜单选择和对话框按钮单击操作时,系统又会相应的发送WM_COMMAND(命令消息)给适当的窗口。命令消息的参数则完全依赖于窗口的菜单设计。您还可以定义一些自己的消息,您的程序可以向

桌面上的任何窗口发送这些消息,这些用户自定义的消息实际上使C++有点像SmallTalk。

您完全用不着担心如何使用这些消息与代码联系起来,因为这是应用程序框架的事情。不过需要注意的是,Windows的这种消息处理机制同事也强加给了用户程序许多固定的结构。因此,不要强迫Windows程序看起来像老式的MS-DOS,只要自己研究一下本书中的例子,读者就不难发现这些程序看起来的确与以往的程序有所不同。

1.1.2Windows的图形设备接口

许多MS-DOS程序都直接往视频存储区或打印机端口输送数据,这种做法的不利之处在于需要对每种显示卡或打印机类型提供相应的驱动程序。Windows则提供了一个抽象的接口,称作图形设备接口(GDI)。Windows已经提供了各种显示卡及打印机的驱动程序,这样我们的程序就可以不必关心与系统相连的显示卡及打印机的类型。我们的程序可以通过调用GDI函数和硬件打交道,而各种GDI函数会自动参考被称为设备环境的数据结构。Windows会自动将设备环境结构映射到相应的物理设备,并且会提供正确的输入/输出指令。GDI在处理速度上几乎和直接进行视频访问一样快,并且它还允许Windows的不同应用程序共享显示器。

1.1.3基于资源的程序设计

在MS-DOS下,为了实现数据驱动程序设计,必须将数据定义为初始化常量,或者提供单独的数据文件供程序读取。而当进行Windows程序设计时,则可以用一些特殊的格式将这些数据存储在资源文件中,这样,连接器就可以把由C++编译器输出的二进制代码和二进制资源文件结合起来生成可执行程序。资源文件可以包含位图、图标、菜单定义、对话框设计和字符串,甚至可以包含用户自定义的格式。

我们可以用文本编辑器来编辑程序,而对各种资源则常用“所见即所得”(wysiwyg)风格的工具来加以编辑。例如,我们在设计对话框时,可以首先从被称为控件箱的图标组中选取适当的元素(按钮、列表框等),然后再通过鼠标来进行定位和尺寸设置。Microsoft Visual C++6.0对各种标准的资源格式都提供了图形资源编辑器。

1.1.4内存管理

Windows的每一个新版本中,内存管理都会比过去跟容易,如果您听过关于锁住内存句柄、形式替换程序以及内存申请管理等很繁琐的细节的话,您现在就不必

担心了,因为这些都过去了。现在我们可以非常简单地申请到所需要的内存,至于细节问题则完全由Windows来控制。第十章介绍Win32所采取的内存管理技术,包括虚拟内存和内存映射文件。

1.1.5动态链接库

在MS-DOS环境下,所有程序的目标模块在创建过程中都被静态地连接起来。而Windows则允许动态地连接,即一些特定结构的库可以再运行过程中被装入和连接,并且多个应用程序可以共享一个动态链接库(DLLs),这样可以大大节省内存和磁盘空间。同时,动态连接还可以大大提高程序的模块灵活性,因为我们可将动态连接库单独编译和调试。

设计者起初创建DLL时完全是为C语言而设计的,C++则使这一问题变得有些复杂。Microsoft基本类库的开发者们已经将应用程序框架的所有类组合进了几个已经建好的动态链接库中,我们可以把应用程序框架类静态或者动态连接到我们的应用程序中,并且我们还以创建自己的基于MFC DLL的MFC扩展DLL。第二十二章详细介绍了如何创建MFC扩展DLL和常规DLL。

1.1.6Win32应用程序编程接口

以前的Windows程序员用C语言在Win16应用程序编程接口(API)上开发应用程序。现在,如果想写32位的应用程序的话,则不管是直接还是间接,都必须使用新的Win32 API。大多数的Win16函数都有对应的Win32函数,但许多参数却有了变化——如16位的参数用32位参数来替代。Win32 API还提供了许多新的函数,包括磁盘I/O函数,而在以前,磁盘操作函数是通过MS-DOS调用完成的。用16位版本的Visual C++,MFC程序员可以不管这些函数调用,因为他们只要按MFC得标准编写程序即可,而MFC的标准在Win16和Win32下都是适用的。

1.2Visual C++的组成

Visual C++包含了两套完整的Windows应用程序开发系统。如果读者愿意,仍然可以使用使用Win32 API来开发C语言写的Windows应用程序。C语言的Win32编程技术在Charles Petazold的《Progamming Windows95》(Microsoft Press,1996)中进行评述。我们可以利用Develop Studio和Visual C++所提供的一些新的工具(包括资源编辑器)使底层Win32程序的编写更加方便。

Visual C++也包括ActiveX模块库(ActiveX Template Library,ATL),我们可以用它来开发在Internet上使用的ActiveXX控件。ATL编程既不是Win32的C

语言编程,也不是MFC的编程,而是非常复杂的,足够写一本书来讲述。

然而本书并不是关于如何使用C语言的Win32来编程,也不是ATL编程,而是关于如何用Visual C++所提供的MFC库应用程序框架来进行C++程序设计。在这里,我们将使用《Microsoft Visual C++ MFC Library Reference》(Microsoft Press, 1997)书中所介绍的C++类,并将会使用应用程序框架的专用Visual C++工具,如AppWizard及ClassWizard等。

说明:使用MFC库程序设计接口并不意味着您就不能使用Windows的Win32函数,实际上,您几乎总是需要在MFC库程序中直接调用Win32函数。

迅速地浏览一下Visual C++的各组成元素将有助于读者理解应用程序框架。图1-1描述了Visual C++应用程序的大致创建过程。

1.2.1Microsoft Visual C++ 6.0和创建过程

Visual Studio是一个运行于Windows及其上的集成开发环境,它是Visual C++6.0、The Visual C++ IDE、Microsoft Visual J++和其他几个产品所公用的开发环境。该IDE来源于原先的Visual Workbench,而Visual Workbench又来源于QuickC for Windows。贴边窗口和可配置的工具栏,再加上一个可运行的可定制编辑器,这就是目前的Visual Studio的组成部分。联机帮助系统的工作方式则像一个Web浏览器。图1-2显示了Visual C++ 6.0工作时的情形。

如果您使用过以前的Visual C++版本或者Borland IDE,那么就已经知道Visual C++ 6.0是如何工作的了,但如果您对集成开发环境还比较陌生的话,则必须弄清楚什么是项目。“项目”是一些相关联的源文件的集合,这些源文件经过编译、连接,然后被组合在一起形成可执行的Windows应用程序或者是DLL。项目源文件一般被存储在一个单独的子目录中,但它也需要依赖于孩子目录之外的许多文件,如包含文件和库文件。

熟练地程序员对于make文件不会感到陌生。make文件包岑了编译器和连接器的参数选项,还表述了所有文件之间的关系(源代码文件需要的特定的包含文件,可执行文件要求包含的目标文件模块及库等)。创建程序(make程序)首先读取make 文件,然后再激活编译器、汇编器、资源编译器和连接器以便产生最好的输出。最好输出并生成的通常是可执行文件。创建程序利用内置的推理规则来激活编译器,以便通过对特定CPP文件的编译来产生特定的OBJ文件。

在Visual C++6.0的项目中,没有make文件(后缀为MAK),除非我们专门输

出一个,但一个文本格式的项目文件(后缀为DSP)有着同样的功能。一个独立的文本格式的工作空间文件(workspace,后缀为DSW)对工作空间中的每一个项目都有一个入口,在一个工作空间中可以有多个项目,不过本书中的所有例子的工作空间只有一个项目。要在一个已经存在的项目上继续工作,只要Visual C++中打开相应的DSW文件,然后就可以编辑和编译该项目了。

Visual C++也会建立许多中间文件,下表列出了在Visual C++工作空间里产生的文件:

1.2.2资源编辑器-Workspace ResourceView

当单击Visual C++的Workspace窗口中的ResourceView标签时,我们就可以选择一个资源进行编辑,主窗口会自动为该类型的资源选择适当的资源编辑器。该窗口会为菜单选择一个所见即得的编辑器,为对话框选择一个功能很强的图形编辑器,并有一些工具可用于编辑图标、位图和字符串。对话框编辑器可以插入标准Windows空间和新的Windows通用控件,也可以插入ActiveX控件。第三章显示了ResourceView的一幅图及一个资源编辑器(对话框编辑器)。

每一个项目通常都有一个文本格式的资源描述(RC)文件,该文件描述了项目的菜单、对话框、字符串和热键资源。RC文件也有#include语句可以将位于其他子目录下的资源包含进来。这些资源包括与该项目相关的项,如位图(BMP)和图标(ICO)文件,以及对所有Visual C++程序通用的资源,如错误信息串等。在资源编辑器以外RC文件进行编辑是不提倡的。资源编辑器也可以EXE喝DLL文件,因此我们可以用剪贴板来“偷”资源,如其他Windows应用程序的位图和图标等。1.2.3C/C++编译器

VisualC++的编译器可以处理C和C++源代码,它通过源代码文件名的后缀来识别代码本身所使用的语言。C后缀代表C源代码,CPP或CXX则代表C++源代码。该编译器与所有的ANSI标准兼容,包括C++库工作组的最新建议,还增加了一些Microsoft扩充内容。VisualC++6.0版还支持模版(template)、异常(exception)和运行时类型识别(runtime typeidentification,RTTI)。新的C++标准模板库(Standard Template Library,STL)也被包括了进来,但它并没有集成到MFC库中。

1.2.4资源编译器

VisualC++资源编译器从资源编辑器读取一个ASCII资源描述(RC)文件,并生成

一个二进制RES文件提供给连接器。

1.2.5连接器

连接器读入由C/C++编译器产生的OBJ文件和有资源编译器的RES文件,同时处理MFC的LIB库文件、运行时库代码及Windows代码,然后产生项目的EXE文件。有一个增量连接选项(incremental link option)可以再源程器变化不大的情况下减少执行的时间。由于MFC头文件中包含的#pragma语句(特殊的编译指示)已指定了所要求的库文件,所以我们不必再告诉连接器该读哪些库。

1.2.6调试器

如果程序被第一运行,则用不着使用调试器,可是以后就少不了一次又一次的使用调试器了。VisualC++调试器的稳定性有了很大提高,但当然仍不可能修正所有的错误。调试器和Develpoer Studio紧密配合以便保证断点可以被保存在磁盘上。通过工具栏按钮可以插入和删除断点,控制单步执行。图1—3显示了正在运行的VisualC++调试器,注意,Variable和Watch窗口可将对象指针展开以便显示出派生类和积累的所有数据成员。如果我们把光标放在简单变量的位置上,则调试器还会在一个小窗口中显示它的值。为了能够对程序进行调试,在创建程序时必须设置相应的编译器和连接器的选项,以便产生相应的可调试信息。

1.2.7AppWizard

AppWizard是一个代码生成器,它会按照用户通过对话框指定的特性、类名及源代码文件名来产生Windows应用程序的工作构架。我们在学习本书中所提供的例子时需要经常使用AppWizard。不过,不要把AppWizard同以前的那种可以为应用产生所有代码的代码生成器混淆起来。AppWizard所产生的代码只是一些基本代码,它所完成的功能则完全由应用程序框架的基类所决定。AppWizard可以使用户很快地开始一个新的应用程序。

高级的用户开发者可以建立定制的AppWizard,Microsoft公司已经公开了用于生成项目的宏系统。如果用户需要建立很多带有远程通讯接口的项目,则可以建立一个特制的向导来自动完成这一过程。

1.2.8ClassWizard

ClassWizard(它是通过DLL来实现的)可以通过Developer Studio的View菜单来运行,它极大的方便了VisualC++类代码的编辑工作。想添加新的类或新的虚拟函数,或新的Windows消息控制函数吗?ClassWizard能够给出原型、函数体以及

将消息同应用程序框架相联系的相应代码。ClassWizard还可以用来对用户自己编写的类的代码进行修改,这就解决了令一般代码生成器感到头疼的维护问题。有些ClassWizard的特性可以通过Developer Studio WizardBar工具栏来实现,见图1-2。

1.2.9源程序浏览器

如果某个程序使我们从头到尾编写的,那么我们一定会对其中的源代码文件、类及成员函数了如指掌,但如果遇到了别人编写的程序,我们若想搞清楚就需要一些帮助了。VisualC++的源程序浏览器(简称“浏览器”)能够使我们从类或函数的角度来了解(和编辑)程序,而不是直接从文件入手,这有点像其他的面向对象库(如Smalltalk)的“检查器”。浏览器有如下几种查看状态:

Definitions and References如果选择任何函数、变量、类型、宏定义或者类,都会马上看到它是在项目中的什么地方定义的,并且在哪些地方用到了它。

Call Graph/Caller Graph—对于所选择的函数,给出它的调用与被调用函数的图示。

Derived Class Graph/Base Class Graph—给出类层次关系的图形表示。对于所选择的类,我们可以看到它的派生类或者基类以及成员,并且可以利用鼠标来控制层次结构的扩展。

File Outlin一对于所选择的文件,列出文件中的类、函数和数据成员,同时还同步显示它们定义的位置和在项.目中使用的位置。

第三章中给出了一个典型的浏览器窗口。

说明:如果我们重新安排了源代码文件中的代码行,则Visual C++6.0会在重新编译连接时更新浏览器数据库。但这会增加编译连接的时间。

除了浏览器之外,Visual C++还有一个C1assView的选项,它不依赖于浏览器数据库。我们可以在ClassView窗口中看到项目中所有类的树状视图,可以显示成员函数和数据成员。在每个小单元上双击鼠标,可以立即切换到相应的源代码。然而,ClassView,不能显示层次信息,而浏览器则可以。

1.2.10联机帮助

Visual C++ 6.0包含一个全新的基于HTML的联机帮助系统。每一个主题都用一个独立的HTML文档来表达,而且所有的主题都组织起来,根据索引文件(IVI),形成了InfoViewer标题文件(IVT) o InfoView窗口使用了从Microsoft Internet

Explorer 4.0引用过来的地址码,所以看起来有点像人们熟知的Web浏览器。Visual C++可以0从VisualC一的CD-ROM默认安装选项)上访问IVT文件,也可以从硬盘上读取,还可以读取Internet上的HTML文件。Visual C++6.0允许以四种方式启动帮助系统:

按书(街book)—当我们从Developer Studi。的Help菜单中选择。intents时,Workspace窗口切换到Infoview模式,这里Developer Studio,Visual C++和Win32 SDK的文档按书和章节的层次被组织在一起。

按主题(脚topic)—当我们从Developer Studio的Help菜单中选择Search并单击In-dex标签时,只要输入一个关键字,就可以看到包含该关键字的主题和文章。按词(By word)—当我们从Developer Studi。的Help菜单中选择,earch并单击Query标签时,只要输人一组词,就可以查到包含这些词的主题和文章。

按Fl(Fl help)—这是对程序员最友好的方式。只要把光标移到函数、宏或者类名上,然后按下F1键,就会启动帮助系统。如果名字在几个地方被发现—例如在MFC和Win32帮助文件中都有—则我们还可以从列表窗口中选择我们所期望的主题。

不管使用什么方式启动联机帮助,我们都可以把帮助中的文本拷贝到剪贴板上,然后插人到我们的程序中去。

1.2.11诊断工具

Visual C++ 6.0一包含一些很有用的的诊断工具。SPY++给出了系统进程、线程和窗口的一

个树形视图,通过它可以观察正在运行的应用程序的窗口及消息。PVIEW(在Windows 95下

为PVIEW95 )对于消除Windows 95的任务列表中看不到的错误进程是非常有用的(Windows

NT Task Manager可以替代PVIEW,用右键单击工具栏可以启动.Task Manager). Visual C ++还包括一整套ActiveX工具、一个ActiveX控件测试程序、HCRTF帮助编译器、一个库管理器、二进制文件查看器和编辑器、一个源码分析器和其他一些工具。唯一所缺少的是Visual C ++6.0的16位版本的DBWIN实用工具。如果我们想查看程序的调试信息,就必须通过调试器来运行程序。

1.2.12源代码控制

Microsoft最近买下了一个称为SourceSafe的现成的源代码控制产品的版

权,该产品包含在Visual C++的企业版中,并被集成进了Visual C++,因此我们可以用它来管理大的软件项目。项目中源代码的主拷贝保存在网络的中枢地点,程序员可以取出(check ouo部分代码来修改,而这些代码通常保存在程序员的本地硬盘上;在程序员放回(check in)了修改的文件后,小组的其他成员就可以同步他们的本地硬盘拷贝与主拷贝一致。其他的一些源代码控制系统也可以集成到Visual C++中来。

1.2.13Gallery

The Visual C++Gallery使用户可以在不同的项目之间共享软件组件。Gallery管理下面三种类型的模块:

ActiveX控件—当我们安装一个ActiveX控件(OCX一以前的OLE控件)后,在Windows的注册表(Registry)中就多了一个条目,所有被注册的ActiveX控件都会出现在illery的窗口里,因此我们可以在任何项目中使用它们。

C ++源程序模块—当我们写了一个新类后,我们可以把它的代码加到Gallery 中,然后这些代码就可以被选择并拷贝到其他的项目里。我们也可以把资源加到Gallery中。

Visual C++6.0组件—Gallery也包含一些工具,我们可以用这些工具为项目加一些特性,这样的工具可能会把新的类、函数、数据成一员和资源插人到一个项目中。

Microsoft提供了一些组件模块作为Visual C ++的一部分(如空闲时间处理、调色板支持和飞溅屏幕( Splash screen)等),其他还有一些由第三方软件厂商提供的组件模块。

说明:如果您决定使用Visual C++。随带的组件模块,则最好先在一个测试用的项目中测试一下该组件,看它是否真正是您想要的。否则,您将很难完全从正式的项目中把加进来的代码去掉。

所有用户产生的Gallery条目都可以从OGX文件中引人,也可以引出到OGX 文件中去。这些文件将作为新的产品发布,并可以作为Visual C一组件被共享。

1.2.14 Microsoft基本类库6.0版

Microsoft基本类库6.0版(简称MFC库)是本书所要讨论的主题,它定义了我们马上就要讲到的应用程序框架。从第二章开始,我们将给读者介绍一些实际的代码及一些重要概念。

1.2.15ActiveX模板库

ATL是一个完全独立于MFC的工具,用它可以建立ActiveX控件。用户既可以用MFC建立ActiveX控件,也可以用ATL建立ActiveX控件,但An控件相对来说又小又快,适合于在Internet上使用。

附件2:外文原文(复印件)

1.1The Windows Programming Model

No matter which development tools you use, programming for Windows is different from old-style batch-oriented or transaction-oriented programming. To get started, you need to know some Windows fundamentals. As a frame of reference, we'll use the

well-known MS-DOS programming model. Even if you don't currently program for plain MS-DOS, you're probably familiar with it.

1.1.1Message Processing

When you write an MS-DOS-based application in C, the only absolute requirement is a function named main. The operating system calls main when the user runs the program, and from that point on, you can use any programming structure you want. If your program needs to get user keystrokes or otherwise use operating system services, it calls an appropriate function, such as getchar, or perhaps uses a character-based windowing library.

When the Windows operating system launches a program, it calls the program's WinMain function. Somewhere your application must have WinMain, which performs some specific tasks. Its most important task is creating the application's main window, which must have its own code to process messages that Windows sends it. An essential difference between a program written for MS-DOS and a program written for Windows is that an MS-DOS-based program calls the operating system to get user input, but a Windows-based program processes user input via messages from the operating system.

Note: Many development environments for Windows, including Microsoft Visual C++ version 6.0 with the Microsoft Foundation Class (MFC) Library version 6.0, simplify programming by hiding the WinMain function and structuring the

message-handling process. When you use the MFC library, you need not write a WinMain function but it is essential that you understand the link between the operating system and your programs.

Most messages in Windows are strictly defined and apply to all programs. For example, a WM_CREATE message is sent when a window is being created, a

WM_LBUTTONDOWN message is sent when the user presses the left mouse button, a WM_CHAR message is sent when the user types a character, and a WM_CLOSE message is sent when the user closes a window. All messages have two 32-bit parameters that convey information such as cursor coordinates, key code, and so forth. Windows sends WM_COMMAND messages to the appropriate window in response to user menu choices, dialog button clicks, and so on. Command message parameters vary depending on the window's menu layout. You can define your own messages, which your program can send to any window on the desktop. These user-defined messages actually make C++ look a little like Smalltalk.

Don't worry yet about how these messages are connected to your code. That's the job of the application framework. Be aware, though, that the Windows message processing requirement imposes a lot of structure on your program. Don't try to force your Windows programs to look like your old MS-DOS programs. Study the examples in this book, and then be prepared to start fresh.

1.1.2The Windows Graphics Device Interface

Many MS-DOS programs wrote directly to the video memory and the printer port. The disadvantage of this technique was the need to supply driver software for every video board and every printer model. Windows introduced a layer of abstraction called the Graphics Device Interface (GDI). Windows provides the video and printer drivers, so your program doesn't need to know the type of video board and printer attached to the system. Instead of addressing the hardware, your program calls GDI functions that reference a data structure called a device context. Windows maps the device context structure to a physical device and issues the appropriate input/output instructions. The GDI is almost as fast as direct video access, and it allows different applications written for Windows to share the display.

1.1.3Resource-Based Programming

To do data-driven programming in MS-DOS, you must either code the data as initialization constants or provide separate data files for your program to read. When you program for Windows, you store data in a resource file using a number of established formats. The linker combines this binary resource file with the C++ compiler's output to generate an executable program. Resource files can include bitmaps, icons, menu definitions, dialog box layouts, and strings. They can even include custom resource formats that you define.

You use a text editor to edit a program, but you generally use wysiwyg (what you see is what you get) tools to edit resources. If you're laying out a dialog box, for example, you select elements (buttons, list boxes, and so forth) from an array of icons called a control palette, and you position and size the elements with the mouse. Microsoft Visual C++ 6.0 has graphics resource editors for all standard resource formats.

1.1.4Memory Management

With each new version of Windows, memory management gets easier. If you've heard horror stories about locking memory handles, thunks, and burgermasters, don't worry. That's all in the past. Today you simply allocate the memory you need, and Windows takes care of the details. Chapter 10 describes current memory management techniques for Win32, including virtual memory and memory-mapped files.

1.1.5Dynamic Link Libraries

In the MS-DOS environment, all of a program's object modules are statically linked during the build process. Windows allows dynamic linking, which means that specially constructed libraries can be loaded and linked at runtime. Multiple applications can share dynamic link libraries (DLLs), which saves memory and disk space. Dynamic linking increases program modularity because you can compile and test DLLs separately.

Designers originally created DLLs for use with the C language, and C++ has added some complications. The MFC developers succeeded in combining all the application framework classes into a few ready-built DLLs. This means that you can statically or dynamically link the application framework classes into your application. In addition,

you can create your own extension DLLs that build on the MFC DLLs. Chapter 22 includes information about creating MFC extension DLLs and regular DLLs. 1.1.6The Win32 Application Programming Interface Early Windows programmers wrote applications in C for the Win16 application programming interface (API). Today, if you want to write 32-bit applications, you must use the new Win32 API, either directly or indirectly. Most Win16 functions have Win32 equivalents, but many of the parameters are different—16-bit parameters are often replaced with 32-bit parameters, for example. The Win32 API offers many new functions, including functions for disk I/O, which was formerly handled by MS-DOS calls. With the 16-bit versions of Visual C++, MFC programmers were largely insulated from these API differences because they wrote to the MFC standard, which was designed to work with either Win16 or Win32 underneath.

1.2The Visual C++ Components

Microsoft Visual C++ is two complete Windows application development systems

in one product. If you so choose, you can develop C-language Windows programs using only the Win32 API. C-language Win32 programming is described in Charles Petzold's book Programming Windows 95 (Microsoft Press, 1996). You can use many Visual C++ tools, including the resource editors, to make low-level Win32 programming easier.

Visual C++ also includes the ActiveX Template Library (ATL), which you can use to develop ActiveX controls for the Internet. ATL programming is neither Win32

C-language programming nor MFC programming, and it's complex enough to deserve its own book.

This book is not about C-language Win32 programming or ATL programming. It's about C++ programming within the MFC library application framework that's part of Visual C++. You'll be using the C++ classes documented in the Microsoft Visual C++ MFC Library Reference (Microsoft Press, 1997), and you'll also be using application framework-specific Visual C++ tools such as AppWizard and ClassWizard.

Note: Use of the MFC library programming interface doesn't cut you off from the Win32 functions. In fact, you'll almost always need some direct Win32 calls in your MFC library programs.

A quick run-through of the Visual C++ components will help you get your bearings before you zero in on the application framework. Figure 1-1 shows an overview of the Visual C++ application build process.

1.2.1Microsoft Visual C++ 6.0 and the Build Process Visual Studio 6.0 is a suite of developer tools that includes Visual C++ 6.0. The Visual C++ IDE is shared by several tools including Microsoft Visual J++. The IDE has come a long way from the original Visual Workbench, which was based on QuickC for Windows. Docking windows, configurable toolbars, plus a customizable editor that runs macros, are now part of Visual Studio. The online help system (now integrated with the MSDN Library viewer) works like a Web browser. Figure 1-2 shows Visual C++ 6.0 in action.

If you've used earlier versions of Visual C++ or another vendor's IDE, you already understand how Visual C++ 6.0 operates. But if you're new to IDEs, you'll need to know what a project is. A project is a collection of interrelated source files that are compiled and linked to make up an executable Windows-based program or a DLL. Source files for each project are generally stored in a separate subdirectory. A project depends on many files outside the project subdirectory too, such as include files and library files.

Experienced programmers are familiar with makefiles. A makefile stores compiler and linker options and expresses all the interrelationships among source files. (A source code file needs specific include files, an executable file requires certain object modules and libraries, and so forth.) A make program reads the makefile and then invokes the compiler, assembler, resource compiler, and linker to produce the final output, which is generally an executable file. The make program uses built-in inference rules that tell it, for example, to invoke the compiler to generate an OBJ file from a specified CPP file.

In a Visual C++ 6.0 project, there is no makefile (with an MAK extension) unless you tell the system to export one. A text-format project file (with a DSP extension)

相关主题
文本预览
相关文档 最新文档