CodeWarrior 使用教程第七课:库和Microsoft Foundation Classes (MFC)



    库是什么?这是一个你必须知道如何回答的问题!一个库就是一个包含了你程序运行时需要调用的函数的文件。在库中提供的典型函数有:文件和屏幕的 I/O 函数,内存管理服务,3-D 图形显示以及科学计算函数。使用库可以缩短你的开发时间并简化你的应用程序。在开发程序当中,你会经常地使用到库。以下是几种在编程中使用库的方法:
•将你需要用到的函数写在一个源文件中,然后把它编译成为一个库或 DLL;  
•使用别人提供的库;
•使用一个其他人提供的共享库(或 DLL)。

编写你自己的库或 DLL

    第一种使用库的方法很简单。你只需在一个 C 或 C++ 源文件中写入源代码,然后编译、连接并执行它。这是我们在本课程中早已学到的知识。就这样你就可以创建你自己的一个库,就这么简单!

使用别人的库

    使用别人提供的库稍稍有些不同。由第三方开发商提供的库通常包括以下几个文件:库文件(通常是以 .lib 为后缀名的文件),一个头文件(后缀名为 .h) 以及(我们希望有但不一定有)一些文档(后缀名为.doc)。头文件是用于让编译器将你的程序中使用的函数与库中的函数匹配起来的。你只需将头文件包括到你的源文件中,然后就可以象调用操作系统的函数一样来调用库函数了。在大多数情况下,通常将 .h 和 .lib 的文件放置于源文件目录中,就像 CodeWarrior 定义的一样。这个路径通常在你的工程文件所在的目录,或者其下的子目录中。需要注意的是,CodeWarrior 提供的某些特定的库,例如它的 MSL,就不是放在你的工程所在的目录中。实际上,CodeWarrior 有一个特定的存取路径,叫做 system paths, 它指向 CodeWarrior 自带的库和头文件存放的目录。

使用一个共享库

    第三方厂商将库函数与它们的产品打包起来发行,而不是以源文件的方式发行这些库。如果你是一个开发商,使用这样的方式来发行你的库会更安全一些,特别是你希望由此来保护你的知识产权时。但是这种方法也存在一个问题,就是如果在库中存在一个 bug 的话,用户是没办法对之进行修改的,只能从开发商那里取得一个修正后的版本。但是这做起来也有许多困难。

共享的 DLL

     DLL 和库文件非常相似,两者之间唯独的不同就是库文件通常都会被编译并连接到你的应用程序中,而 DLL 则是存放在你的系统目录下或者是在编译后的应用程序的目录下的一个独立的文件。当应用程序运行时,它会自己去找到该 DLL 并使用它的服务。DLL 文件的后缀名为 .dll。

使用共享的DLLs 的好处

    DLL 的优点就是它能够被共享。那么,共享的好处在哪里呢?首先,可以让多个应用程序使用一个 DLL。当几个程序都使用一个占用很大磁盘和内存空间的 DLL 时,就很有用了。另外,如果一个 DLL 包含了一个 bug,然后被修正了,那么所有使用这个 DLL 的程序都会被自动更新,这样就可以大大地节约你的开发时间。那么哪种库应该由多个应用程序共享呢?比如操作系统中的每一个视窗就是使用共享的 DLL 得来的。

使用共享 DLL 需要注意的地方

    CodeWarrior IDE 的 CD-ROM 中有许多库,在因特网上还有成千上万的库。记住,当你使用第三方厂商的库时,千万要先查查毒。这就如同出门前要关门一样的有必要。

    CodeWarrior 自带的库分为两类。一个是 MSL,它提供 ANSI C/C++ 的标准函数。MSL 已经被移植到许多平台上了,因此可以在 Windows,Mac OS 和 Solaris 系统中使用它的函数。另一个是供特定平台使用的库。例如,在 Windows 版的光盘中,你可以发现提供了使用 Windows APIs 和 MFC 库的Windows 32 支持库,它提供了编写面向对象的 Windows 应用程序的框架。随着你的编程经验的增长,你将发现这些特定库的更多的用途。


MFC是什么?

    Microsoft Foundation Classes (MFC) 提供了快速开发 Windows 应用程序的函数集。虽然你已经可以自由地直接调用 Win32 APIs,但使用 MFC 会更简单一些,因为 MFC 提供了诸如创建一个窗口或者使用智能默认设置和错误检查代码来编写文件这样的基本服务。MFC 可以在 Windows 版的 CodeWarrior 光盘中找到,但不一定是最新的版本。你可以查看 Metrowerks 的帮助页面来获得相关信息,因为微软公司会为 CodeWarrior 的发行版本提供一些升级补丁。

    对于特定平台,比如Macintosh 的开发者可以使用 Metrowerks 自己的 PowerPlant 类库。PowerPlant 是为编写 Mac 应用程序而设计的面向对象的应用程序框架。我们在本课程中并不打算讨论 PowerPlant,但你应知道,它与 MFC 在许多方面是类似的。你在这里学到的知识也将帮助你理解 PowerPlant。我们讨论 MFC 时,其中的许多概念也可以应用到 PowerPlant 上。


注意: 在 Macintosh 版的 CodeWarrior 光盘中也有一个 MFC 的库文件,但不像 Windows 版的光盘,这里不包含源代码。使用 MFC,你可以编写跨平台的代码,还可以同时为 Macintosh(使用 PowerPlant) 和 PC(使用 MFC) 编译这个代码。是不是很酷?

应用程序框架

    就像前面提到过的一样,MFC 是一个应用程序框架。也就是说,MFC 是一个源代码或库格式的 C++ 类集。使用这个框架可以在较短时间内创建一个支持高级操作系统功能的完整的应用程序。此外,MFC 还包含了用以构造你的用户界面的工具。这些工具,特别是图象编辑器、对话框编辑器和资源编译器,能够帮助你构造用户界面中的图形元素(在本课最后一节中将解释如何安装这些有用的组件)。一个应用程序框架只涉及到应用程序的标准用户界面,而与该应用程序的特有的内容无关。换句话说,这个框架能够帮助你构造一个功能强大的用户界面,把你的时间节省下来以便让你有更多时间去编写实现应用程序目的的其它函数。因此你不必担心会花很多时间来创建一个和其它一些运行在操作系统环境中具有同样 Windows 化界面的应用程序,因为你只需去运用那些库就可以达到这些目的了。

图 7-1 MFC 的用户界面构造器

应用程序框架还能处理分发消息(例如用户击键的动作、鼠标点击、绘画消息等等)到应用程序中的许多按钮、列表、窗口和控件上。这个功能帮助你能够集中注意力到实现应用程序的功能而不是界面上,把那些创建界面的工作留给应用程序框架吧。

    应用程序框架提供了许多超过老式风格的 roll-your-own 方法。首先,这些代码是成熟和经过严格测试的。每天都有成百上千的开发者使用 MFC 来开发应用程序。每当 Windows 操作系统增加了一些新的功能的时候,MFC 也会不断地被升级,你可以通过使用新版本的 MFC 来编译你的应用程序,以便获得所有新的功能特色,而且几乎不用对应用程序的源代码做任何修改。

    应用程序框架还提供可重用的代码。代码重用是象 C++ 这样的面向对象的编程方法得以发展的主要原因之一。通过将可重用的代码封装为类,你可以很容易地使用已经存在的类来添加需要的功能。代码重用使得你可以使用经过严格测试的代码来构造你的应用程序,并很容易获得预期的功能。而且你还可以很容易地将你编写的类与其它需要同样功能的人进行共享。

    与你自己创建所需框架而言,使用应用程序框架有时会使得你的程序稍大一些。因为现代计算机通常都配置了足够数量的内存,而且这个框架可以作为一个 DLL 来使用,所以增加这么一些程序体积还是很值得的。PowerPlant 和 MFC 是两个很值得你去熟练使用的高效的工具。    

如何安装和使用 MFC 接口工具

    MFC 接口工具(包括对话编辑器和图象编辑器)可以在 CodeWarrior 安装光盘上找到。但是,它们并不是作为 CodeWarrior 安装程序的一部分存在的,因此在完成了 CodeWarrior 的安装后,在硬盘上你还是找不到 MFC。为了安装这些工具,请按照以下步骤进行:
•找到 iTOOLS.Exe 文件。这是安装 MFC 接口工具必须的安装程序。iTools.Exe 文件可以在 C:\Program Files\Metrowerks\CodeWarrior\BinSDK\bin\ 目录下找到。如果在那儿找不到这个文件,那么就到 CodeWarrior 的安装光盘上去找。该文件应该在 Extras\SKDs\Win32\Microsoft Win32 SDK Tools\目录下。


注意: 该安装文件 (iTOOLS.Exe) 将会询问你想在哪里保存 MFC 接口工具。但不行的是,它却没有提供定位目录用的浏览(browser)按钮可用,因此你必须手工的输入一个路径。如果你并不能确定你输入的路径是否就是你想要保存这些工具的路径,你可以先在那个目录中找到一个文件,然后通过查看该文件的属性(在该文件上点击右键然后选择 Properties 项),从这个属性框上你可以复制到完整的路径,并把它填入到安装过程中提示要输入安装路径的编辑框中。
•这些工具将要被安装到目的安装路径的 \bin\ 目录中。在这个路径中你将可以找到对话框编辑器(DlgEdit.Exe),图象编辑器(ImagEdit.Exe)和其它一些有用的工具;
•如果你正在开发 Windows 95 或 Windows NT,需要的工具可以在 \bin\win95 和 \bin\winnt 目录中找到。


附原文:

Libraries

What's a library? This is a question you need to know the answer to! A library is a file that contains compiled code that your programs can call upon when needed. Typical functions that libraries can offer are file and screen I/O, memory management services, 3-D graphics display, and scientific computations. Libraries can save you time in the development process and help simplify your software applications. Chances are that you will use libraries quite often in developing programs. There are several ways to use library code in your programs:
•Write it yourself in a source code file, and optionally compile it as a library or DLL.
•Use a library provided by someone else.
•Use a shared library (a DLL) provided by someone else.

Writing Your Own Library or DLL

The first method is simple. When you include code within a C or C++ source file, you complete the program by simply compiling, linking, and executing it. This is the way we've been working thus far during this course. See, you were using them and you didn't even know it. You're good.

Using Someone Else's Library

Using someone else's library changes things a bit. Libraries provided by third-party vendors contain several files: the library file itself (usually with a name ending in .lib), a header file (.h), and (hopefully) some documentation (.doc). The header file is required so that the compiler can match function calls in your program to functions within the library. You include the header file in your source file and then simply call the library functions as if they were part of the operating system. In most cases, you place the .h and .lib files in your source path, as defined by CodeWarrior. This path is usually in the directory where your project file is located, or inside a subdirectory of this directory. Note that certain libraries CodeWarrior provides, such as its MSL, reside in a different directory from the one your project lives in. In fact, CodeWarrior has its own special access paths, called system paths, which point to the directories of its own libraries and header files.

Using a Shared Library

Third-party vendors ship library files with their products so that they don't have to part with their source code. If you're the vendor, it's much safer to include the library and a header file (as opposed to a C or C++ source file and a header file), especially if you want to protect your intellectual property. The problem with this approach is that if there's a bug in the library you've gotten from a third-party vendor, you'll be unable to fix it yourself. You'll have to obtain a fix for the library from that vendor, and who knows how easy that will be?

Sharing DLLs

A DLL is very similar to a library file. In fact, the only difference is that a library file usually gets compiled and linked into your application, while a DLL is a separate file that either sits in your system directory or in the same directory as your compiled application. When your application runs, it locates the DLL and uses its services on the fly. DLLs are typically identified by the .dll extension.

Benefits of Sharing DLLs

The benefit of a DLL is that it can be shared. OK, that was pretty obvious. What is the benefit of sharing? First, more than one application can make use of a DLL. This can be especially useful when several programs use a large DLL that takes up lots of disk space and/or memory. Also, if a DLL contains a bug and is updated, all programs that share it will be automatically updated. This saves you and your run-time processing large amounts of time. So what kind of library would need to be shared by multiple applications? Oh, stuff like the very windows you see in your operating system environment.

Note on Using Shared DLL files

The CodeWarrior IDE CD-ROM contains dozens of libraries, and hundreds more are available on the Internet. Always, always, always run a virus check or disinfectant program before incorporating third party code into your product. It's a good habit, like locking the door every time you leave the house.

The libraries included with CodeWarrior fall into two categories. The first is the MSL, which provides ANSI C/C++ standard functions. The MSL has been ported to many platforms, so its functions are available in various flavors of Windows, the Mac OS, and Solaris. A second set of libraries is intended for platform-specific uses. For example, on the Windows CD, you'll find Windows 32 support libraries that interface to the Windows APIs and the MFC libraries, which provide an object-oriented application framework for writing Windows applications. When you gain more programming experience, you will find more uses for the specialized libraries.


What is MFC?

The Microsoft Foundation Classes (MFC) provides a set of functions that let you quickly write Windows applications. While you're free to call the Win32 APIs directly, it's easier to use the MFC because the classes provide basic services such as creating a window or writing a file along with intelligent default settings and error-checking code. MFC is available on the Windows CodeWarrior CD but may not be the very latest version of the classes. Check with the Metrowerks help desk for that information, as Microsoft will send update patches between CodeWarrior releases.

For the sake of platform parity, Macintosh developers can use Metrowerks' own PowerPlant class libraries. PowerPlant is an object-oriented application framework designed for writing Mac applications. Although we will not discuss PowerPlant here, it and MFC are similar in many ways, and what you learn here will help you to understand PowerPlant as well. When I discuss MFC, it is safe to assume that the same concepts apply to PowerPlant.


Note: There is also a library version of MFC on the CodeWarrior Macintosh CD, but unlike the Windows CD, the source code is not included for the Macintosh. Using MFC, you can write cross-platform code. Once you have written the code, you can compile it for both the Macintosh (using PowerPlant) and the PC (using MFC) at one time. Pretty cool, huh?

Application Framework

As mentioned earlier, MFC is an application framework. This means that MFC is a collection of C++ classes in source code or library format. The framework can create a complete application that supports advanced operating system features in a fraction of the time it would take to do it all by hand. Note also that MFC contains tools to facilitate building your user interface. These tools, specifically the Image Editor, Dialog Editor, and Resource Compiler, help you build the graphic elements of the user interface (the last section of the lesson explains how to install these useful components). An application framework concerns itself primarily with the standard user interface of the application, as opposed to the specific content of the application. Put another way, the framework helps you build a robust application interface to interact with the user, giving you more time to write the other functions that implement the application's purpose. So you don't have to worry about spending time and energy coding a scroll bar or close box from scratch to appear like all other applications running in the operating system environment. You can just use the libraries and get on with the real creative stuff.

Figure 7-1 MFC's user interface builder.

Application frameworks also handle the dispatching of messages (such as user keystrokes, mouse clicks, drawing messages, etc.) to the many buttons, lists, windows, and controls in your application. This allows you to keep your application's code focused on its own features rather than on addressing interface questions like "How do I tell when the user pressed the Enter key?" or "How do I know when I need to redraw the contents of a window?" It's really the little things that mean the most once you are actually using the application.

Application frameworks provide many benefits over the old-style roll-your-own approach. For one thing, the code is mature and well tested. Hundreds of applications are being built by hundreds of developers using MFC every single day. It is constantly being updated as new features are added to the Windows operating system. In many cases, you can recompile your application with a new version of MFC and take advantage of all the new features with few if any changes to your source code.

Application frameworks also provide reusable code. Code reuse is one of the main reasons object-oriented coding methodologies like C++ were developed. By writing code in reusable classes, you can easily build upon existing classes to add necessary features. Code reuse allows you to depend on well-tested code throughout your application and feel confident that the code will work as expected. It is also very easy to share classes that you build with others who may need the same features.

Application frameworks can sometimes make your programs a bit larger than they might be if you were to create them completely from scratch. Since modern computers are usually equipped with substantial amounts of memory, and since the framework can be used as a DLL, it's fairly easy to justify the added bulk. PowerPlant and MFC are both very powerful tools that you should consider using.

Your best bet now is to focus on learning more about MFC and how it can assist you -- and you can make the decision on how much you want done for you, or how much you want to dig in and do yourself.

How to Install and Use MFC Interface Tools

The MFC interface tools (including Dialog Editor and Image Editor) are located on the CodeWarrior CD. Unfortunately, they're not part of the CodeWarrior Installation, so after the initial CodeWarrior installation, you won't find them on your hard drive. In order to install the tools, simply perform the following steps:
•You need to locate the file named iTOOLS.Exe. This is the installation program you will use to install the MFC Interface Tools. The iTools.Exe file should be located at C:\Program Files\Metrowerks\CodeWarrior\BinSDK\bin\. If you can't find the file there, then go to the CodeWarrior CD. The file iTOOLS.Exe can be located in the folder Extras\SKDs\Win32\Microsoft Win32 SDK Tools\.


Note: The Installation program (iTOOLS.Exe) will ask you where you want to save the MFC Interface Tools. Unfortunately, there is no Browse button to locate a folder location, so you will have to manually type in a directory path. If you are unsure of the exact path for where you want to save the tools, locate a file that resides in the folder where you want to save the tools and retrieve the complete path for this folder by examining the file's properties (right-click on the file and select Properties). Here's a quick tip: you can actually copy the path from this Properties box and then paste the path when prompted by the installation program.
 •The tools will be located in the \bin\ folder of the target installation folder. The \bin\ folder is where you'll find the Dialog Editor (DlgEdit.Exe), Image Editor (ImagEdit.Exe), and other useful tools.
•If you're developing applications for Windows 95 or Windows NT, the necessary tools are available in the folders \bin\win95 and \bin\winnt, respectively.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
======================================================================== MICROSOFT FOUNDATION CLASS LIBRARY : Test ======================================================================== AppWizard has created this Test application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your Test application. Test.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. Test.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CTestApp application class. Test.cpp This is the main application source file that contains the application class CTestApp. Test.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. Test.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\Test.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file Test.rc. res\Test.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// AppWizard creates one dialog cla

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值