有关“Windows编程模式”的中英文翻译

上传人:仙*** 文档编号:30117627 上传时间:2021-10-09 格式:DOC 页数:16 大小:52.51KB
收藏 版权申诉 举报 下载
有关“Windows编程模式”的中英文翻译_第1页
第1页 / 共16页
有关“Windows编程模式”的中英文翻译_第2页
第2页 / 共16页
有关“Windows编程模式”的中英文翻译_第3页
第3页 / 共16页
资源描述:

《有关“Windows编程模式”的中英文翻译》由会员分享,可在线阅读,更多相关《有关“Windows编程模式”的中英文翻译(16页珍藏版)》请在装配图网上搜索。

1、附录1 外文原文8The Windows Programming ModelNo 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, well use the well-known MS-DOS

2、 programming model. Even if you dont currently program for plain MS-DOS, youre probably familiar with it. Message ProcessingWhen 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 fro

3、m 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

4、 a program, it calls the programs WinMain function. Somewhere your application must have WinMain, which performs some specific tasks. Its most important task is creating the applications main window, which must have its own code to process messages that Windows sends it. An essential difference betw

5、een 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. NOTEMany development environments for Windows, including Microsoft

6、 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

7、 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 w

8、hen 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 choic

9、es, dialog button clicks, and so on. Command message parameters vary depending on the windows 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. Dont worry yet about how t

10、hese messages are connected to your code. Thats the job of the application framework. Be aware, though, that the Windows message processing requirement imposes a lot of structure on your program. Dont try to force your Windows programs to look like your old MS-DOS programs. Study the examples in thi

11、s book, and then be prepared to start fresh. The Windows Graphics Device InterfaceMany 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

12、a layer of abstraction called the Graphics Device Interface (GDI). Windows provides the video and printer drivers, so your program doesnt 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 dat

13、a 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. Resource-Based Pr

14、ogrammingTo 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

15、resource file with the C+ compilers 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 wy

16、siwyg (what you see is what you get) tools to edit resources. If youre 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 gra

17、phics resource editors for all standard resource formats. Memory ManagementWith each new version of Windows, memory management gets easier. If youve heard horror stories about locking memory handles, thunks, and burgermasters, dont worry. Thats all in the past. Today you simply allocate the memory y

18、ou need, and Windows takes care of the details. Chapter 10 describes current memory management techniques for Win32, including virtual memory and memory-mapped files. Dynamic Link LibrariesIn the MS-DOS environment, all of a programs object modules are statically linked during the build process. Win

19、dows 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 sepa

20、rately. 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 cl

21、asses 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. The Win32 Application Programming InterfaceEarly Windows programmers wrote applications in C for the Win16

22、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 different16-bit parameters are often replaced with 32-bit parameters, f

23、or 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 wo

24、rk with either Win16 or Win32 underneath. BitmapsWithout graphics images, Microsoft Windows-based applications would be pretty dull. Some applications depend on images for their usefulness, but any application can be spruced up with the addition of decorative clip art from a variety of sources. Wind

25、ows bitmaps are arrays of bits mapped to display pixels. That might sound simple, but you have to learn a lot about bitmaps before you can use them to create professional applications for Windows. This chapter starts with the old way of programming bitmapscreating the device-dependent GDI bitmaps th

26、at work with a memory device context. You need to know these techniques because many programmers are still using them and youll also need to use them on occasion. Next youll graduate to the modern way of programming bitmapscreating device-independent bitmaps (DIBs). If you use DIBs, youll have an ea

27、sier time with colors and with the printer. In some cases youll get better performance. The Win32 function CreateDIBSection gives you the benefits of DIBs combined with all the features of GDI bitmaps. Finally, youll learn how to use the MFC CBitmapButton class to put bitmaps on pushbuttons. (Using

28、CBitmapButton to put bitmaps on pushbuttons has nothing to do with DIBs, but its a useful technique that would be difficult to master without an example.) GDI Bitmaps and Device-IndependentBitmapsThere are two kinds of Windows bitmaps: GDI bitmaps and DIBs. GDI bitmap objects are represented by the

29、Microsoft Foundation Class (MFC) Library version 6.0 CBitmap class. The GDI bitmap object has an associated Windows data structure, maintained inside the Windows GDI module, that is device-dependent. Your program can get a copy of the bitmap data, but the bit arrangement depends on the display hardw

30、are. GDI bitmaps can be freely transferred among programs on a single computer, but because of their device dependency, transferring bitmaps by disk or modem doesnt make sense. NOTEIn Win32, youre allowed to put a GDI bitmap handle on the clipboard for transfer to another process, but behind the sce

31、nes Windows converts the device-dependent bitmap to a DIB and copies the DIB to shared memory. Thats a good reason to consider using DIBs from the start. DIBs offer many programming advantages over GDI bitmaps. Because a DIB carries its own color information, color palette management is easier. DIBs

32、 also make it easy to control gray shades when printing. Any computer running Windows can process DIBs, which are usually stored in BMP disk files or as a resource in your programs EXE or DLL file. The wallpaper background on your monitor is read from a BMP file when you start Windows. The primary s

33、torage format for Microsoft Paint is the BMP file, and Visual C+ uses BMP files for toolbar buttons and other images. Other graphic interchange formats are available, such as TIFF, GIF, and JPEG, but only the DIB format is directly supported by the Win32 API.Color Bitmaps and Monochrome BitmapsNow m

34、ight be a good time to reread the Windows Color Mapping section in Chapter 5. As youll see in this chapter, Windows deals with color bitmaps a little differently from the way it deals with brush colors. Many color bitmaps are 16-color. A standard VGA board has four contiguous color planes, with 1 co

35、rresponding bit from each plane combining to represent a pixel. The 4-bit color values are set when the bitmap is created. With a standard VGA board, bitmap colors are limited to the standard 16 colors. Windows does not use dithered colors in bitmaps. A monochrome bitmap has only one plane. Each pix

36、el is represented by a single bit that is either off (0) or on (1). The CDC:SetTextColor function sets the off display color, and SetBkColor sets the on color. You can specify these pure colors individually with the Windows RGB macro. Using GDI BitmapsA GDI bitmap is simply another GDI object, such

37、as a pen or a font. You must somehow create a bitmap, and then you must select it into a device context. When youre finished with the object, you must deselect it and delete it. You know the drill. Theres a catch, though, because the bitmap of the display or printer device is effectively the display

38、 surface or the printed page itself. Therefore, you cant select a bitmap into a display device context or a printer device context. You have to create a special memory device context for your bitmaps, using the CDC:CreateCompatibleDC function. You must then use the CDC member function StretchBlt or

39、BitBlt to copy the bits from the memory device context to the real device context. These bit-blitting functions are generally called in your view classs OnDraw function. Of course, you mustnt forget to clean up the memory device context when youre finished. The Effect of the Display Mapping ModeIf t

40、he display mapping mode in the Red Blocks example is MM_TEXT, each bitmap pixel maps to a display pixel and the bitmap fits perfectly. If the mapping mode is MM_LOENGLISH, the bitmap size is 0.54-by-0.96 inch, or 52-by-92 pixels for Windows 95, and the GDI must do some bit crunching to make the bitm

41、ap fit. Consequently, the bitmap might not look as good with the MM_LOENGLISH mapping mode. Calling CDC:SetStretchBltMode with a parameter value of COLORONCOLOR will make shrunken bitmaps look nicer. 附录2 中文译文Windows编程模式无论使用哪一种开发工具,在Windows环境下编成都不同于旧式的面向批处理或者面向事务处理的编程。在开始前,需要了解一些Windows基本知识。作为参考。我们将使

42、用众所周知的MS-DOS程序,也可能熟悉它。消息处理当用C语言编写基于MS-DOS的应用程序时,唯一绝对需要的是一个名为main的函数。当用户运行程序时,操作系统调用main,并且,从这里开始,可以使用任何需要的编程结构。如果程序需要获得用户键击或者使用操作系统服务,他便调用适当的函数,例如getchar,或者可能使用一个基于字符的窗口库。当Windows操作系统启动一个程序时,他调用程序的WinMain函数。在一些地方,应用程序必须有WinMain,它执行一些特定的任务。它最重要的任务是创建应用程序的主窗口,它必须有自己的代码来处理Windows发送给它的信息。在MS-DOS程序和Windo

43、ws程序之间,一个基本区别是MS-DOS程序调用操作系统来获取用户输入,但是,Windows程序通过来自操作的信息来处理用户输入。注意:许多Windows的开发环境,包括带有MFC库6.0版本的Microsoft Visual C+ 6.0,通过隐藏WinMain函数和结构化消息处理过程来简化编成。当使用MFC库时,就不必编写WinMain函数,但是理解操作和程序之间的联系是至关重要的。Windows中的大部分消息是严格定义的,而且适用于所有的程序。例如,当创建一个窗口时,就会发送一个VM-CREAT消息;当用户按下鼠标左键时,会发送WM-LBUTTONDOWN消息;还有,当关闭窗口时,将发送

44、一个WM-CLOSE消息。所有消息具有两个32位参数,他们传送诸如光标坐标、键代码这样的信息。Windows对适当的窗口发送WM-COMMOND消息,以响应用户菜单选择、对话按钮的单击等等。命令消息参数随着窗口菜单的布局而有所不同。用户可以定义自己的消息,这些消息的确使C+看起来有点像Smalltalk。但是,不要担心这些消息怎样与代码相关。这是应用程序框架的工作。应当清楚的是,Windows消息处理要求在程序上强加了许多结构。研究本书的例程,然后准备开始 。Windows 图形设备接口许多MS-DOS程序直接写显存和打印机接口。这种技术的不利之处是对每一种视频板和每一种打印机型号,需要其支持

45、的驱动程序软件。Windows 引入了一个名为图形设备接口(GDI)的抽象化外层。Windows提供视频和打印驱动程序,所以,用户不必知道有关系统的视频卡和打印机的类型。程序不是寻址硬件,而是调用(GDI)函数,这些函数引用名为设备上下文(device context)的数据结构。Windows把设备上下文结构映射到物理设备,并且发出适当的输入/输出指令。图形设备接口几乎与直接视频访问一样快,并且它允许不同的Windows应用程序来共享显示。基于资源的编成要在MS-DOS环境下进行数据驱动编程,必须或者尾巴数据编码成为初始化常量或者提供独立的数据文件让程序来读。进行Windows编程时,使用大

46、量已经确立的格式在资源文件中存储数据。链接程序把二进制资源文件连接到C+编译器的数出来产生一个可执行文件。资源文件可以包括位图、图标、菜单定义、对话框外观和字符串。他们甚至可以包括自定义的定制资源格式。使用一个文本编译器来编译一个程序,但是一般使用外语siwyg(所见即所得)工具来编译资源。例如,如果正在布置一个对话框,从控制面板的图标序列来选定元素(按钮、列表框等),并且用图标来确定元素的位置和大小。Microsoft Visual C+有全部标准资源格式的图形资源编辑器。内存管理使用Windows的每一个新的版本,内存管理变得更加容易。如果所说过关于锁定内存句柄、形实转换程序和伯格氏管理器

47、(burgermaster)的恐怖故事,不要担心。这全部是过去的事情了。今天简单地分配所需要的内存,而Windows处理细节问题。第10章描述了Win32内存管理技术,包括虚拟内存和内存映射。动态链接库在MS-DOS环境下,一个程序的所有对象模块在建立过程中是静态链接的。Windows允许动态链接,这意味着特别创建的库可以在运行时加载和链接。多个应用程序可以共享动态链接库(DLLs),它节省内存和磁盘空间。动态链接增加了程序的模块性,因为可以单独编译和测试动态链接库。设计者最初使用C语言创建动态链接库,并且C+增加了一些复杂性。MFC开发者成功地把所有应用程序框架类与少量已经建立好的动态链接库

48、结合。这意味着可以静态或者动态把应用程序框架类连接到应用程序。另外,可以通过在MFC动态链接库基础上建立,创建自己的扩充动态链接库。第22章包括有关创建MFC扩充动态链接库和常规动态链接库。Win32 应用程序编程接口早期Windows程序员使用C语言编写Win16应用程序编程接口的应用程序。今天,如果需要编写32位应用程序,必须直接地或间接地使用新的Win32应用程序编程接口。大多数Win16函数都有对应的Win32函数,但是许多参数都不一样了。比如说,16位参数通常被32位参数所取代。Win32应用程序编程接口提供了许多新的函数,包括磁盘输入/输出函数,它们以前是由MS-DOS调用处理的。

49、使用Visual C+的16位版本,MFC程序员很大程度上不接触这些应用程序编程接口的区别,因为它们按照MFC标准编写程序,这个标准是在Win16或者Win32环境下面设计的。位图如果没有图形图像。基于Microsoft Windows的应用程序就会变得十分单调。一些应用程序依靠图像来实现他们的用途,但是任何应用程序都可以用来各种来源的装饰剪辑艺术而打扮起来。Windows位图是映射到显示像素的位数组。这听起来可能很简单,但是在可以使用为图为Windows创建专业的应用程序之前,必须学习许多关于位图的知识。本章以位图编程的“老”方法做为开始,创建使用于内存设备相关的依靠设备的GDI位图。你需要

50、了解这些技术,原因是许多程序员仍然在使用它们。下一步将涉及到为位图编成的现代方法:创建与设备无关的位图(DIB)。如果使用DIB,那么就会更加轻松自如地处理颜色和打印机。在某些情况中,将获得更好的性能,Win32函数CreatDIBSection提供了与GDI位图的所有特征结合起来的DIB为图所带来的好处。最后,将学会如何使用MFC CBitmapButton类在按钮上放置位图(使用CBitmapButton 在按钮上放置位图与DIB无关,但是它是一项十分有用的技术,不使用例子掌握它是很困难的)。GDI位图和设备无关的位图有两种类型的Windows位图:GDI位图和DIB。GDI位图对象是由M

51、icrosoft Foundation Class(MFC)Library 6.0版本的CBitmap类所代表的。GDI位图对象有一个与之关联的Windows数据结构,它在Windows GDI模块内进行维护,它是依赖于设备的。程序可以获得位图数据的副本,但是为排列则取决于显示硬件。GDI位图可以自由地在单一计算机上的程序之间进行传输,但是由于它们是依赖于设备的,所以通过磁盘或调制解调器来传输位图是没有意义的。 注意:在Win32种,允许你在剪贴板上放置GDI位图句柄,以便把它传输给另一个进程,但在幕后,Windows将依赖于设备的位图转换为DIB,并将这个DIB复制到共享的内存,这就是一开始

52、就考虑使用DIB的一个很好理由。与GDI位图相比,DIB提供了许多编程方面的优点。因为DIB携带有自己色颜色信息,所以调色板管理起来更加容易一些。DIB还使得在打印时更加易于控制阴影。运行Windows的任何计算机都可以处理DIB,它通常是存储在BMP磁盘文件之中,或者作为资源存储在程序的EXE和DLL文件中。监视器上的墙纸背景是在你启动Windows的时候从BMP文件中读取的。Microsoft画笔的主要存储格式是BMP文件,Visual C+使用BMP文件用于工具栏按钮和其他图像。另外还可以提供其他图形交换格式,例如TIFF、GIF和JPEG,但是只有DIB格式是直接由Win32 API支

53、持的。彩色位图和单色位图现在可能是重新阅读第5章中“Windows颜色映射”一节的好时候了。正如你将在本章中所看到的那样,Windows处理彩色位图的方式与处理画刷颜色略有不同。许多彩色位图是16位色的。一个标准的VGA板具有四个连续的彩色平面。来自每个平面的一个对应位,组合起来代表一个像素。当创建位图的时候,设置4位颜色值。对于标准的VGA板,位图颜色被限制在标准的16色。Windows不在位图中使用抖动的颜色。单色位图只有一个平面。每个像素用一个单一位来代表,该位或者是0或者是1。CDC:SetTextColor函数可以设置“off”显示颜色,SetBkColor可以设置“on”颜色。你可

54、以用Windows RGB宏来指定这些纯色。使用GDI位图GDI位图中只是另一个GDI对象,例如钢笔或字体。你必须先创建一个位图,然后选中它进入设备上下文。当你完成对该对象的操作之后,必须解除对它的选中并删除它。尽管如此,仍有一些问题,原因是显示或打印机设备的“位图”是有效的显示表面或打印的页面本身。因此,你不能选中一个位图显示设备上下文或打印机设备上下文。然后,你必须使用CDC成员函数StretchBit或BitBit来从内存设备上下文中向“真正”设备上下文中复制位。这些“位复制”函数一般在查看类的OnDraw函数中调用。当然,你千万不能忘记在完成之后清除内存设备上下文。显示映射模式的效果如果在Red Blocks例子中的显示映射模式MW-TEXT,那么每个位图像素映射为一个显示像素,并且位图匹配的很好。如果映射模式是MW-LOENGLISH,那么位图大小将是0.540.96英寸,或5292像素(对于Windows95),为了使位图合适,GDI必须作一些位处理。结果导致了位图看上去没有采用MW-LOENGLISH映射模式是好。用COLORONCOLOR的参数值调用CDC:SetStretchBltMo降使得不太好看的位图变得更好看一些。

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!