Copyright© 1998 by Charles Petzold
Copyright© 1998 by Charles Petzold
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright (c) 1998 by Charles Petzold
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by
any means without the written permission of the publisher.
Library of Congress Cataloging-in-Publication Data
Petzold, Charles, 1953-
Programming Windows / Charles Petzold. -- 5th ed.
p. cm.
Rev. ed. of: Programming Windows 95.
Includes index.
ISBN 1-57231-995-X
1. Microsoft Windows (Computer file) 2. Operating systems
(Computers) I. Petzold, Charles, 1953-. Programming Windows 95.
II. Title.
QA76.76.O63P533 1998
005.265--dc21 98-42529
CIP
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 WCWC 4 3 2 1 0 9
Distributed in Canada by Penguin Books Canada Limited.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further information
about international editions, contact your local Microsoft Corporation office or contact Microsoft Press
International directly at fax (425) 936-7329. Visit our Web site at mspress.microsoft.com
.
Macintosh, QuickTime, and TrueType fonts are registered trademarks of Apple Computer, Inc. Intel is a
registered trademark of Intel Corporation. Developer Studio, DirectX, IntelliMouse, Microsoft, Microsoft Press,
Microsoft QuickBasic, MS-DOS, MSDN, Natural, Visual Basic, Visual C++, Visual Studio, Windows, and Windows
NT are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other
countries. Other product and company names mentioned herein may be the trademarks of their respective
owners.
The names of example companies, products, people, characters, and/or data mentioned herein are fictitious
and are in no way intended to represent any real individual, company, product, or event, unless otherwise
noted.
Acquisitions Editor:
Ben Ryan
Project Editor:
Devon Musgrave
Technical Editor:
Dail Magee, Jr.
Copyright© 1998 by Charles Petzold
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
"10
th
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
Copyright© 1998 by Charles Petzold
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.
Copyright© 1998 by Charles Petzold
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 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.
评论1