有关Windows编程模式的中英文翻译.doc

上传人:仙人指路1688 文档编号:2393801 上传时间:2023-02-17 格式:DOC 页数:15 大小:49KB
返回 下载 相关 举报
有关Windows编程模式的中英文翻译.doc_第1页
第1页 / 共15页
有关Windows编程模式的中英文翻译.doc_第2页
第2页 / 共15页
有关Windows编程模式的中英文翻译.doc_第3页
第3页 / 共15页
有关Windows编程模式的中英文翻译.doc_第4页
第4页 / 共15页
有关Windows编程模式的中英文翻译.doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《有关Windows编程模式的中英文翻译.doc》由会员分享,可在线阅读,更多相关《有关Windows编程模式的中英文翻译.doc(15页珍藏版)》请在三一办公上搜索。

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位图。你需要了解这些技术,原因是许多程序员仍然在使用它们。下一步将涉及到为位图编成的现代方法:创建与设备无关的位图(DIB)。如果使用DIB,那么就会更加轻松自如地处理颜色和打印机。在

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号