Programming Windows - Win32 Api (MSpress, Charles Petzold, 5th Ed).pdf

(6831 KB) Pobierz
2_[Charles Petzold] Programming Windows - Win32 API 5th Ed
This document is created with the unregistered version of CHM2PDF Pilot
Copyright 1998 by Charles Petzold
 
4439452.001.png
This document is created with the unregistered version of CHM2PDF Pilot
Author's Note
Visit my web site www.cpetzold.com for updated information regarding this book, including possible bug reports
and new code listings. You can address mail regarding problems in this book to charles@cpetzold.com . Although
I'll also try to answer any easy questions you may have, I can't make any promises. I'm usually pretty busy, and my
cat refuses to learn the Windows API.
I'd like to thank everyone at Microsoft Press for another great job in putting together this book. I think this "10th
Anniversary Edition" of Programming Windows is the best edition yet. Many other people at Microsoft (including
some of the early developers of Microsoft Windows) also helped out when I was writing the earlier editions, and
these fine people are listed in those editions.
Thanks also to my family and friends, and in particular those more recent friends (you know who you are!) whose
support has made this book possible. To you this book is dedicated.
Charles Petzold
October 5, 1998
 
This document is created with the unregistered version of CHM2PDF Pilot
Chapter 1
Getting Started
This book shows you how to write programs that run under Microsoft Windows 98, Microsoft Windows NT 4.0,
and Windows NT 5.0. These programs are written in the C programming language and use the native Windows
application programming interfaces (APIs). As I'll discuss later in this chapter, this is not the only way to write
programs that run under Windows. However, it is important to understand the Windows APIs regardless of what
you eventually use to write your code.
As you probably know, Windows 98 is the latest incarnation of the graphical operating system that has become the
de facto standard for IBM-compatible personal computers built around 32-bit Intel microprocessors such as the 486
and Pentium. Windows NT is the industrial-strength version of Windows that runs on PC compatibles as well as
some RISC (reduced instruction set computing) workstations.
There are three prerequisites for using this book. First, you should be familiar with Windows 98 from a user's
perspective. You cannot hope to write applications for Windows without understanding its user interface. For this
reason, I suggest that you do your program development (as well as other work) on a Windows-based machine
using Windows applications.
Second, you should know C. If you don't know C, Windows programming is probably not a good place to start. I
recommend that you learn C in a character-mode environment such as that offered under the Windows 98 MS-DOS
Command Prompt window. Windows programming sometimes involves aspects of C that don't show up much in
character-mode programming; in those cases, I'll devote some discussion to them. But for the most part, you should
have a good working familiarity with the language, particularly with C structures and pointers. Some knowledge of
the standard C run-time library is helpful but not required.
Third, you should have installed on your machine a 32-bit C compiler and development environment suitable for
doing Windows programming. In this book, I'll be assuming that you're using Microsoft Visual C++ 6.0, which can
be purchased separately or as a part of the Visual Studio 6.0 package.
That's it. I'm not going to assume that you have any experience at all programming for a graphical user interface such
as Windows.
 
This document is created with the unregistered version of CHM2PDF Pilot
The Windows Environment
Windows hardly needs an introduction. Yet it's easy to forget the sea change that Windows brought to office and
home desktop computing. Windows had a bumpy ride in its early years and was hardly destined to conquer the
desktop market.
A History of Windows
Soon after the introduction of the IBM PC in the fall of 1981, it became evident that the predominant operating
system for the PC (and compatibles) would be MS-DOS, which originally stood for Microsoft Disk Operating
System. MS-DOS was a minimal operating system. For the user, MS-DOS provided a command-line interface to
commands such as DIR and TYPE and loaded application programs into memory for execution. For the application
programmer, MS-DOS offered little more than a set of function calls for doing file input/output (I/O). For other tasks
in particular, writing text and sometimes graphics to the video display applications accessed the hardware of the PC
directly.
Due to memory and hardware constraints, sophisticated graphical environments were slow in coming to small
computers. Apple Computer offered an alternative to character-mode environments when it released its ill-fated Lisa
in January 1983, and then set a standard for graphical environments with the Macintosh in January 1984. Despite the
Mac's declining market share, it is still considered the standard against which other graphical environments are
measured. All graphical environments, including the Macintosh and Windows, are indebted to the pioneering work
done at the Xerox Palo Alto Research Center (PARC) beginning in the mid-1970s.
Windows was announced by Microsoft Corporation in November 1983 (post-Lisa but pre-Macintosh) and was
released two years later in November 1985. Over the next two years, Microsoft Windows 1.0 was followed by
several updates to support the international market and to provide drivers for additional video displays and printers.
Windows 2.0 was released in November 1987. This version incorporated several changes to the user interface. The
most significant of these changes involved the use of overlapping windows rather than the "tiled" windows found in
Windows 1.0. Windows 2.0 also included enhancements to the keyboard and mouse interface, particularly for menus
and dialog boxes.
Up until this time, Windows required only an Intel 8086 or 8088 microprocessor running in "real mode" to access 1
megabyte (MB) of memory. Windows/386 (released shortly after Windows 2.0) used the "virtual 86" mode of the
Intel 386 microprocessor to window and multitask many DOS programs that directly accessed hardware. For
symmetry, Windows 2.1 was renamed Windows/286.
Windows 3.0 was introduced on May 22, 1990. The earlier Windows/286 and Windows/386 versions were merged
into one product with this release. The big change in Windows 3.0 was the support of the 16-bit protected-mode
operation of Intel's 286, 386, and 486 microprocessors. This gave Windows and Windows applications access to up
to 16 megabytes of memory. The Windows "shell" programs for running programs and maintaining files were
completely revamped. Windows 3.0 was the first version of Windows to gain a foothold in the home and the office.
Any history of Windows must also include a mention of OS/2, an alternative to DOS and Windows that was
originally developed by Microsoft in collaboration with IBM. OS/2 1.0 (character-mode only) ran on the Intel 286
(or later) microprocessors and was released in late 1987. The graphical Presentation Manager (PM) came about
with OS/2 1.1 in October 1988. PM was originally supposed to be a protected-mode version of Windows, but the
 
This document is created with the unregistered version of CHM2PDF Pilot
graphical API was changed to such a degree that it proved difficult for software manufacturers to support both
platforms.
By September 1990, conflicts between IBM and Microsoft reached a peak and required that the two companies go
their separate ways. IBM took over OS/2 and Microsoft made it clear that Windows was the center of their strategy
for operating systems. While OS/2 still has some fervent admirers, it has not nearly approached the popularity of
Windows.
Microsoft Windows version 3.1 was released in April 1992. Several significant features included the TrueType font
technology (which brought scaleable outline fonts to Windows), multimedia (sound and music), Object Linking and
Embedding (OLE), and standardized common dialog boxes. Windows 3.1 ran only in protected mode and required
a 286 or 386 processor with at least 1 MB of memory.
Windows NT, introduced in July 1993, was the first version of Windows to support the 32-bit mode of the Intel
386, 486, and Pentium microprocessors. Programs that run under Windows NT have access to a 32-bit flat address
space and use a 32-bit instruction set. (I'll have more to say about address spaces a little later in this chapter.)
Windows NT was also designed to be portable to non-Intel processors, and it runs on several RISC-based
workstations.
Windows 95 was introduced in August 1995. Like Windows NT, Windows 95 also supported the 32-bit
programming mode of the Intel 386 and later microprocessors. Although it lacked some of the features of Windows
NT, such as high security and portability to RISC machines, Windows 95 had the advantage of requiring fewer
hardware resources.
Windows 98 was released in June 1998 and has a number of enhancements, including performance improvements,
better hardware support, and a closer integration with the Internet and the World Wide Web.
Aspects of Windows
Both Windows 98 and Windows NT are 32-bit preemptive multitasking and multithreading graphical operating
systems. Windows possesses a graphical user interface (GUI), sometimes also called a "visual interface" or "graphical
windowing environment." The concepts behind the GUI date from the mid-1970s with the work done at the Xerox
PARC for machines such as the Alto and the Star and for environments such as SmallTalk. This work was later
brought into the mainstream and popularized by Apple Computer and Microsoft. Although somewhat controversial
for a while, it is now quite obvious that the GUI is (in the words of Microsoft's Charles Simonyi) the single most
important "grand consensus" of the personal-computer industry.
All GUIs make use of graphics on a bitmapped video display. Graphics provides better utilization of screen real
estate, a visually rich environment for conveying information, and the possibility of a WYSIWYG (what you see is
what you get) video display of graphics and formatted text prepared for a printed document.
In earlier days, the video display was used solely to echo text that the user typed using the keyboard. In a graphical
user interface, the video display itself becomes a source of user input. The video display shows various graphical
objects in the form of icons and input devices such as buttons and scroll bars. Using the keyboard (or, more directly,
a pointing device such as a mouse), the user can directly manipulate these objects on the screen. Graphics objects
can be dragged, buttons can be pushed, and scroll bars can be scrolled.
The interaction between the user and a program thus becomes more intimate. Rather than the one-way cycle of
information from the keyboard to the program to the video display, the user directly interacts with the objects on the
display.
Users no longer expect to spend long periods of time learning how to use the computer or mastering a new program.
Windows helps because all applications have the same fundamental look and feel. The program occupies a window
 
Zgłoś jeśli naruszono regulamin