PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-639 9
Copyright © 1999 by Jeff Prosis e
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means withou t
the written permission of the publisher .
Library of Congress Cataloging-in-Publication Data
Prosise, Jeff.
Programming Windows with MFC / Jeff Prosise. -- 2nd ed.
p. cm.
Rev. ed. of: Programming Windows 95 with MFC.
ISBN 1-57231-695-0
1. Microsoft Windows (Computer file) 2. Operating Systems
(Computers) 3. Microsoft foundation class library. I. Prosise,
Jeff. Programming Windows 95 with MFC. II. Title.
QA76.73.B3P77 1999
005.265--dc21 99-13028
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 ITP Nelson, a division of Thomson 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 internationa l
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 is a registered trademark of Apple Computer, Inc. Intel is a registered trademark of Intel Corporation. ActiveX ,
Microsoft, Microsoft Press, MS-DOS, PowerPoint, Visual Basic, Visual C++, Windows, and Windows NT are either registere d
trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and compan y
names mentioned herein may be the trademarks of their respective owners .
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks .
The example companies, organizations, products, people, and events depicted herein are fictitious. No association with an y
real company, organization, product, person, or event is intended or should be inferred .
Acquisitions Editor: Eric Stroo
Project Editor: Sally Stickney
Technical Editor: Marc Young
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks .
Acknowledgments
The production of this book required the efforts of many people, but two in particular deserve to be singled out for their
diligent, sustained, and unselfish efforts. Sally Stickney, the book's principal editor, navigated me through that minefield called
the English language and contributed greatly to the book's readability. Marc Young, whose talents as a technical editor are
nothing short of amazing, was relentless in tracking down bugs, testing sample code, and verifying facts. Sally, Marc: This
book is immeasurably better because of you. Thanks.
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks .
Introduction
Like many of my colleagues in this industry, I learned Windows programming from Charles Petzold's Programming
Windows —a classic programming text that is the bible to an entire generation of Windows programmers. When I set out t o
become an MFC programmer in 1994, I went shopping for an MFC equivalent to Programming Windows . After searching in
vain for such a book and spending a year learning MFC the old-fashioned way, I decided to write one myself. It's the book you
hold in your hands. And it's the book I would like to have had when I was learning to program Windows the MFC way.
MFC, as you probably already know, is Microsoft's C++ class library for Windows programming. Programming Windows with
MFC isn't a book about C++; rather, it's a book about writing 32-bit Windows applications in C++ using MFC rather than the
Windows API as the chief means of accessing the operating system's essential features and services. It was written with two
kinds of people in mind:
Windows API programmers who want to learn MFC
Programmers who have never before programmed Windows
Whichever camp you fall into, I assume that you know the C++ programming language already and are comfortable with basic
C++ idioms such as derived classes and virtual functions. If these assumptions are true, you're ready to begin climbing the hill
that is MFC programming.
Even veteran Windows programmers frequently find MFC code confusing the first time they see it, in part because of the
presence of code created by the MFC code-generating wizards in Visual C++ and in part because of the countless lines of
code hidden away in MFC classes such as CFrameWnd , CDocument , and CView . That's why this book takes a rather unusua l
approach to teaching MFC. It begins by having you write MFC code by hand (without the wizards) and by utilizing MF C
1.0-style application architectures—that is, applications that use neither documents nor views. Only after you've mastered th e
fundamentals and become acquainted with basic MFC classes such as CWnd and CWinApp do I introduce the wizards and
teach you how to take advantage of MFC's document/view architecture. Along the way, you build a understanding from the
ground up of the message-oriented nature of Windows and of key components of Windows itself, such as the Graphics Device
Interface (GDI). I believe that this approach makes learning MFC not only less intimidating, but also more enjoyable. I think
that you'll agree once you've worked your way through the book and can look back on the learning experience from the
standpoint of a knowledgeable Windows programmer.
Programming Windows with MFC is divided into four parts. Part I introduces the core tenets of MFC and Window s
programming, beginning with a simple "Hello, MFC" application and introducing, one by one, menus, controls, dialog boxes ,
and other application building blocks. Part II builds on the foundation laid in Part I with a detailed look at the document/vie w
architecture. In particular, Chapters 9, 10, and 11 reveal much of the "magic" behind documents and views and explain no t
only how to write basic document/view applications but also how to implement some not so basic features such a s
split-window views of a document and print previews. Part III covers some of the more advanced features of Windows an d
MFC—features such as color palettes, bitmap handling, and multiple threads of execution. In Part IV, you'll learn how MF C
wraps its arms around COM, OLE, and ActiveX and how to write COM-enabled applications and software components. By th e
time you're finished with Chapter 21, you'll be well versed in the art of 32-bit Windows programming using MFC. And you'l l
have prodigious amounts of sample code to draw from when it's time to strike out on your own and write your first grea t
Windows application .
What's New in the Second Edition
Those of you who read the first edition of this book will notice two rather obvious changes in the second edition. First, thi s
edition contains seven new chapters. One is devoted to the MFC view classes; another covers the MFC collection classes; on e
introduces MFC file I/O and serialization mechanisms; and four cover the relationship between MFC and COM. MFC is not th e
general-purpose COM framework that the Active Template Library (ATL) is, but MFC makes certain types of CO M
programming exceptionally easy. For example, MFC greatly simplifies the task of writing ActiveX controls, and it makes writin g
Automation servers—programs that use COM to expose their functionality to scripting clients—a breeze .
This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks .