1、附录 1 外文原文 8 The 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, well use the well-known MS
2、-DOS programming model. Even if you dont currently program for plain MS-DOS, youre probably familiar with it. Message 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, an
3、d 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 lau
4、nches 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
5、 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 Mic
6、rosoft 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 th
7、e 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
8、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
9、 choices, 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
10、 how these 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
11、in this book, and then be prepared to start fresh. The 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 intr
12、oduced 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 referenc
13、e 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. Resource-B
14、ased 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
15、 binary 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 generall
16、y use wysiwyg (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
17、 has graphics resource editors for all standard resource formats. Memory Management With 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
18、 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. Dynamic Link Libraries In the MS-DOS environment, all of a programs 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