第二章
The Microsoft Foundation Class Library Application Framework
微软应用程序基础框架类库
This chapter introduces the Microsoft Foundation Class (MFC) library 7.0 Application Framework and explains its benefits. It includes a stripped-down but fully operational MFC library program for Microsoft Windows that should help you understand what application framework programming is all about. I’m keeping theory to a minimum here, but I’ve included sections on message mapping and on documents and views to help you understand the examples in later chapters.
这章主要讲解微软应用程序基础框架类库和它的益处.我将把MFC类库的运做完全的展现出来,并使你明白应用框架程序是帮你建立WINDOWS程序的.我在这里将保持很少的纯理论论述,但我将在消息映射,文档和视图部分的章节后面帮你弄些例子使你明白.
Why Use The Application Framework?
为什么要使用应用框架
If you’re going to develop applications for Windows, you’ve got to choose a development environment. Assuming that you’ve already rejected non-C options such as Microsoft Visual Basic and Borland Delphi, here are some of your remaining options:
Program in C with the Win32 API.
Writ your own C++ Windows class library that uses Win32.
Use the MFC library application framework
Use another Windows-based application framework. (Most of Them
Are defunct, however-such as Borland’s Object Windows Library [OWL].)
如果你要开发WINDOWS的应用程序,你将选择开发环境.骄傲的你也许已经放弃了非C的的环境,比如,VB,DELPHI,这里有些其它的选择:
用C语言写基于WIN32的程序
使用C++写基于WIN32的WINDOWS类库
使用MFC应用程序框架
使用其他基于WINDOWS的应用框架(许多其它的框架已经消失了,然而例如宝兰的OWL确活的很好).
NOTE We’ll cover .NET Windows Forms in Part VI of this book.
解释:在我们书的第六部分包括.NET的Windows 的窗体.
If you’re starting from scratch, any option will involve a big learning curve. If you’re already a Win32 programmer, you’ll still have a learning curve with the MFC library. Since its release, the MFC library has become the dominant Windows class library. But even if you’re familiar with it it’s still a good idea to step through the features of this programming choice.
如果你从头开始看本书,你心中应该有个大概的学习曲线.如果你已经是WIN32的高手,你仍将学习一下MFC库,因为在最新的版本中MFC已经占据了Windows类库中的统治地位,即使你只是熟悉一下它,MFC提供特色仍旧是你选择写出良好程序的好的选择.
The MFC library is the low-level C++ interface to the Windows API
C++ has been a standard for many developers for a number of years. It’s turned into a mature, well-understood framework with great third-party support. If you need the highest-performance applications, your applications must live as closely to the Windows API as possible. C++ and MFC are as close as you can get without writing WndProc methods by hand.
MFC库是基于低级C++语言的Windows API的接口
C++是许多开发者支持许多年的标准.它是成熟的良好的框架,得到许多第三方的支持.如果你需要高效的应用程序,你的应用程序必须接近Windows API的运行的可能.C++和MFC已不在支持你去写WNDPROC这样的方法.
Application framework applications use a standard structure
Any programmer starting on a large project develops some kind of structure for the code. The problem is that each programmer’s structure is different, and it’s difficult for a new team member to learn the structure and conform to it. The MFC library application framework includes its own application structure—one that’s been proven in many software environments and in many projects. If you write a program for Windows that uses the MFC library, you can safely retire to a Caribbean island, knowing that your minions can easily maintain and enhance your code back home.
应用程序框架是应用程序开发的标准结构
每个程序员在开始一个大的项目时都有一些结构代码.这里有个问题,就是每个程序员有不同的结构,并且新的成员很难学会这个结构并符合它.MFC库应用程序框架在我们的程序中只包含一个结构,并在很多软件环境和许多项目中得到检验.如果你用MFC写基于Windows应用程序,你能安全的撤离加勒比海的小岛,在你回家后你的仆人能很容易继续维护和提高你的代码.
Don’t think that the MFC library’s structure makes your programs inflexible. With the MFC library, your program can call Win32 functions at any time, so you can take maximum advantage of Windows.
不要以为MFC库结构的会使你的程序死板.用MFC库,你的程序可以在任何地方调用Win32函数,所以你在Windows中有最大的有利条件
Application framework applications are small and fast
Back in the 16-bit days, you could build a self-contained Windows EXE file that was less than 20 KB in size. Today, Windows-based programs are larger. One reason is that 32-bit code is fatter. Even with the large memory model, a Win16 program uses 16-bit addresses for stack variables and many globals. Win32 programs use 32-bit addresses for everything and often use 32-bit integers because they’re more efficient than 16-bit integers. In addition, the C++ exception-handling code consumes a lot of memory.
使用应用程序框架的应用程序小而快
在16位的时代,你只能建立小于20KB的Windows的EXE文件.今天,基于Windows的程序已经很大了.一个常用的32位代码是很庞大的.即使是使用大的内存空间,Win16的程序也只能使用16-bit的公用的堆栈变量.Win32的程序可以为每个程序使用32-bit地址并且使用32-bit整数,因为它比16-bit整数更加的有效率.另外,C++的异常操作会消耗大量的内存.
That old 20KB program didn’t have a docking toolbar, splitter windows, print preview capabilities, or control container support—features that users now expect. MFC programs are bigger because they do more and look better. Fortunately, it’s now easy to build applications that dynamically link to the MFC code(and to C run-time code), so the size goes back down again—from 192KB to about 20KB! Of course, you’ll need some big support DLLs in the background , but those are a fact of life these days.
旧的20KB的程序不支持工具栏的停靠,窗口分隔,打印预览,或者是容器控件的支持,现在使用者可以使用这些特色了.MFC的程序是很大的因为它更能干更美观.幸运的是,它现在很容易的建立应用程序并动态连接到MFC的代码(同时也到C运行时的代码),所以你的程序的大小又可以从192K回到20K.当然,你的程序需要一些大的DLL在背后支持,但是这个事实在目前来说还无法改变.
As far as speed is concerned, you’re working with machine code produced by an optimizing compiler. Execution is fast, but you might notice a startup delay as the support DLLs are loaded.
至于速度的问题,你的机器代码已经被编译器最优化了.执行起来很快,但是你可能在程序开始加载DLL时感觉到有所延迟.
The Visual C++.NET tools reduce coding drudgery
The Visual C++.NET resource editors, the MFC Application Wizard, and the code wizards available from Class View significantly reduce the time needed to write code that’s specific to your application. For example, the resource editor creates a header file that contains assigned values for #define constants. The MFC Application Wizard generates skeleton code for your entire application, and you can use the Properties window to add message handlers and map messages to them.
Visual C++的自带工具可以减少编码的难度
Visual C++自带的资源编辑器,应用程序向导,类代码生成器向导可以大幅度的减少我们编码的时间.例如:用资源编辑器建立头文件中用#define定义的常数.MFC的应用程序向导可以为你的程序建立完整的框架代码,并且你能用属性窗口中增加消息头跟消息映射.
The MFC library application framework is feature rich
The MFC library 1.0 classes, introduced with C/C++7.0, included the following features:
A C++ interface to the Windows API
General-purpose (non-Windows-specific) classes, including:
Collection classes for lists, arrays, and maps
A useful and efficient string class
Time, time span, and date classes
File access classes for operating system independence
Support for systematic object storage and retrieval to and from disk
A “common root object” class hierarchy
Streamlined Multiple Document Interface(MDI) application support
Some support for OLE1.0
MFC类库应用程序框架的发展历史:
MFC1.0的类库,引入了C/C++7.0,包括以下特色:
Windows API是C++语言封装的接口
通用性(非Windows 类)¸包括:
集合类包括,列表,数组和映射
有效,有用的字符串类
时间,时间计算,日期类
独立与操作系统的文件访问类
支持系统对象从磁盘存储跟恢复.
一个”同根对象”的类层次
应用程序支持新型的多文档接口.
支持少数的OLE1.0
The MFC library 2.0 classes (in Visual C++ 1.0) picked up where the version 1.0 classes left off by supporting many user interface features that are found in current Windows-based application, plus they introduced the application framework architecture. Here’s a summary of the important new features:
Full support for File Open, Save and Save as commands and the most recently used file list
Print preview and printer support
Support for scrolling windows and splitter windows
Support for toolbars and status bars
Access to Visual Basic controls
Support for context-sensitive help
Support for automatic processing of data entered in a dialog box
An improved interface to OLE 1.0
DLL support
MFC2.0类库(在Visual C++ 1.0)在原来1.0的基础上被精简了,当前程序发现在没有它支持的许多用户接口的特色上,正引入了应用程序框架体系.这里大概介绍以下几个重要的特色:
完全支持文件打开,保存,另存为很最近用户打开的文件列表特色
打印预览跟打印机的支持.
支持窗口的滚动跟窗口分隔.
支持工具栏跟状态栏
访问VB的控件
支持灵敏的上下文帮助
支持在对话框中数据录入的自动处理
改进了OLE 1.0的接口
支持DLL
The MFC library 2.5 classes (in Visual C++1.5) contributed the following:
Open Database Connectivity (ODBC) support that allows you application to access and update data stored in many popular databases such as Microsoft Access, Microsoft FoxPro, and Microsoft SQL Server
An interface to OLE 2.01,with support for in-place editing, linking, Drag and drop, and OLE Automation
MFC2.5版(在Visual C++1.5中)有下列贡献:
开放数据库互连它是个允许你的程序支持许多通用数据库的访问和支持,
例如Microsoft Access, Microsoft FoxPro,和Microsoft SQL Server.
增强OLE2.01接口,支持编辑,连接,拖放,和OLE自动化.
Visual C++2.0 was the first 32-bit version of the product. It included support for Microsoft Windows NT version 3.5. It also contained the MFC library 3.0, which had the following new features:
Tab dialog box (property sheet) support (which was also added to Visual C++ 1.51, included on the same CD)
Docking control bars that were implemented within MFC
Support for thin-frame windows
A separate Control Development Kit (CDK) for building 16-bit and 32-bit OLE controls, although no OLE control container support was provided
Visual C++2.0是第一个32位版本的产品.它支持Windows NT3.5版.它包含MFC3.0的库.以下是它的几个新特征:
Tab对话框(属性页)支持(在Visual C++1.51中也增加了,在同一个CD里)
停靠栏也被加入了MFC
支持轻框架窗口
用于建立16位和32位OLE控件的单独的控件开发包,已不需要OLE控件容器的支持了.
A subscription release, Visual C++2.1 with the MFC library 3.1, added the following:
Support for the new Windows 95 (beta) common controls
A new ODBC Level 2 driver integrated with the Access Jet database engine
Winsock classes for TCP/IP data communication
使用MFC3.1版的Visual C++2.1的发布,增加了下列特性:
支持新的Windows 95(beta)通用控件
集成了JET数据库引擎新的ODBC
使用TCP/IP协议数据通讯的Winsock类.
Microsoft decided to skip Visual C++3.0 and proceeded directly to 4.0 in order to synchronize the product version with the MFC library. The MFC library 4.0 contains these additional features:
New OLE-based Data Access Object (DAO) classes for use with the Jet engine
Use of the Windows 95 docking control bars instead of the MFC control bars
Full support for the common controls in the released version of Windows 95, with new tree view and rich-edit view classes
New classes for thread synchronization
OLE control container support
Microsoft决定跳过Visual C++3.0并继续开发MFC4.0的类库.MFC4.0增加了以下特征:
使用JET引擎的基于新的OLE数据访问对象的类.
使用Windows 95的停靠拦控件代替了MFC的控件
完全支持Windows 95的公共控件,及新的树控件跟RichEdit控件.
新的同步线程类
OLE容器支持
Visual C++4.2 was and important subscription release that included the MFC 4.2. The following new features were included:
WinInet classes
ActiveX Documents Server classes
ActiveX synchronous and asynchronous moniker classes
Enhanced MFC ActiveX Control classes, with features such as windowless activation, optimized drawing code, and so forth
Improved MFC ODBC support, including recordset bulk fetches and data transfer without binding.
Visual C++ 4.2是个重要的版本,它包括MFC4.2.它包括以下新的特征:
WinInt 类
ActiveX文档服务类
ActiveX同步和异步类
增强的MFC ActiveX类,它包括了灵活的无窗,最优化的绘图代码等等.
改良了MFC ODBC的支持,这包括取记录集的大小和无绑定的数据传输.
Visual C++ 5.0 included the MFC library 4.21, which fixed some 4.2 bugs. Visual C++5.0 introduced some worthwhile features of its own as well:
A redesigned IDE, Microsoft Developer Studio 97, which included and HTML-based online help system and integration with other languages, including Java
The Active Template Library (ATL) for efficient ActiveX control construction for Internet
C++ language support for COM(Component Object Model) client programs with the new #import statement for type libraries, as described in Chapter25.
Visual C++5.0包括MFC4.21,它修复了一些4.2的错误.Visual C++5.0包括一些很突出的特点:
从新设计了IDE, Microsoft Developer Studio 97,它包含了基于HTML在线帮助系统并集成了其他语言,包括Java
在第25章将描述C++语言支持的COM(组件对象模型)客户程序,可以使用新的#import语句来声明类型库.
Visual C++ 6.0 includes the MFC library 6.0 (Notice that the versions are synchronized again.) Many of the features in the MFC library 6.0 enabled developers to support the modern platform at the time, the Microsoft Active Platform, including the following:
MFC classes that encapsulate the new Windows common controls introduced as part of Microsoft Internet Explorer 4.0
Support for Dynamic HTML(DHTML),which allows MFC programmers to create applications that can dynamically manipulate and generate HTML pages
Active Document Containment, which allows MFC-based applications to contain Active Documents
OLE DB Consumers and Providers Template support and ActiveX Data Objects (ADO) data binding, which help database developers who use the MFC library or ATL
Visual C++6.0包括MFC6.0(又是同步发部的版本),MFC6.0有许多新的特征帮助开发人员支持当时最新的平台,Microsoft活动平台,包括下列特色:
MFC封装了新的Windows通用控件同时引入了Microsoft IE4.0
支持动态的HTML(DHTML),允许MFC的程序员在建立应用程序时能动态的操作和产生HTML页.
活动文档容器,允许基于MFC的应用程序包含活动文档
OLE 客户和提供者摸板支持 和ADO数据绑定,可以帮助那些数据库开发者使用MFC和ATL
The latest edition of Visual C++, Visual C++.NET ,includes the MFC library 7.0. Many of the features in the MFC library 7.0 support Internet programming (and the new Microsoft .NET platform) and also improve the Windows development environment. The new features include the following:
Enhanced Support for HTML help within MFC applications
Support for windowless controls
DHTML dialog boxes and editing components
HTTP argument management classes
Windows 2000 Print dialog box
Stricter message-handler type checking
Date support beyond the year 2038
最近的Visual C++版本,Visual C++.NET包含MFC7.0.拥有许多特色的MFC7.0支持Internet编程(在新的Microsoft .NET平台)并同样改进了Windows开发环境.新的特色如下:
增强了MFC应用程序HTML帮助
支持无窗口控件
DHML对话框和编辑组件
HTTP讨论管理类
Windows 2000的打印对话框
严格的消息管理类型检查机制
日期最远支持到2038年
本文地址:http://com.8s8s.com/it/it742.htm