`executable files
`on CD-ROM
`
`++=
`
`SECOND EDITION
`VERSION 1.5
`
`~RVID J. \
`KRU6l1N~KI
`
`1
`
`GOOGLE-1006
`
`
`
`SECOND EDITION
`VERSION 1.5
`
`~RVID J.
`KRU6l1N~KI
`
`2
`
`
`
`PUBLISHED BY
`Microsoft Press
`A Division of Microsoft Corporation
`One Microsoft Way
`Redmond, Washington 98052-6399
`
`Copyright © 1994 by David]. Kruglinski
`
`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
`Kruglinski, David].
`Inside visual C++ I David]. Kruglinski. -- 2nd ed.
`p.
`cm.
`Includes index.
`: $39.95 ($53.95 Can.)
`ISBN 1-55615-661-8
`2. Microsoft Visual C++.
`1. C++ (Computer program language)
`I. Title.
`QA76.73.C153K78 1994
`005.265--dc20
`
`94-19212
`CIP
`
`Printed and bound in the United States of America.
`
`23456789 MLML 987654
`
`Distributed to the book trade in Canada by Macmillan of Canada, a division of Canada Publishing
`Corporation.
`
`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 (206) 936-7329.
`
`TrueType is a registered trademark of Apple Computer, Inc. ToolBook is a registered trademark of
`Asymetrix Corporation. Borland is a registered trademark of Borland International, Inc. CASE:W is a
`trademark of Case works, Inc. TETRIS is a trademark ofvlo Electronorgtechnica. Hewlett-Packard and
`LaserJet are registered trademarks of Hewlett-Packard Company. Informix is a registered trademark
`of Informix Software, Inc. Ingres is a trademark of Ingres Corporation. Intel is a registered trademark
`and Pentium is a trademark ofIntel Corporation. Lego is a registered trademark of Lego Systems,
`Inc. FoxPro, Microsoft, Microsoft Access, Microsoft QuickBasic, and Visual Basic are registered
`trademarks and Visual C++, Windows, and the ·Windows operating system logo are trademarks of
`Microsoft Corporation. 3M is a registered trademark and Post-it is a trademark of Minnesota Mining
`and Manufacturing Corporation. ORACLE is a registered trademark of Oracle Corporation.
`PowerBuilder is a trademark of Powersoft Corporation. Small talk is a registered trademark of Xerox
`Corporation. All other trademarks and service marks are the property of their respective owners.
`
`Acquisitions Editor: Dean Holmes
`Project Editor: Jack Litewka
`Technical Editor: Jim Fuchs
`
`3
`
`
`
`CONTENTS SUMMARY
`
`ACKNOWLEDGMENTS .......................... , . . . . . . . . . . . . . . . xxiii
`INTRODUCTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. xxv
`
`PART I WINDOWS, VISUAL C++, AND APPLICATION
`FRAMEWORK FUNDAMENTALS . . . . . . . . . . . . . . . . . . . .. 1
`CHAPTER ONE
`MICROSOFT WINDOWS AND VISUAL C++. . . . . . . . . . . . . . . .. 3
`CHAPTER TWO
`THE MICROSOFT FOUNDATION CLASS LIBRARY
`APPLICATION FRAMEWORK. . . . . . . . . . . . . . . . . . . . . . . . . .. 15
`
`PART II THE MFC LIBRARY VIEW CLASS . . . . . . . . . . . . . . . . . .. 27
`CHAPTER THREE
`GETTING STARTED WITH APPWIZARD-"HELLO, WORLD!" .. 29
`CHAPTER FOUR
`BASIC EVENT HANDLING-USING CLASSWIZARD . . . . . . . .. 51
`CHAPTER FIVE
`THE GRAPHICS DEVICE INTERFACE (GDI). . . . . . . . . . . . . . .. 75
`CHAPTER SIX
`THE MODAL DIALOG ................................ 107
`CHAPTER SEVEN
`THE MODELESS DIALOG AND
`THE COMMDLG DIALOG CLASSES
`CHAPTER EIGHT
`VISUAL BASIC CONTROLS .. . . . . . . . . . . . . . . . . . . . . . . . .. 149
`CHAPTER NINE
`WINDOWS MEMORY MANAGEMENT-JUST SAY "NEW" .,. 167
`CHAPTER TEN
`BITMAPS ........ , . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 181
`CHAPTER ELEVEN
`BITMAP BUTTONS, THE TIMER,
`AND ON-IDLE PROCESSING .......................... 195
`
`133
`
`PART III THE DOCUMENT-VIEW ARCHITECTURE. . . . . . . . . .. 213
`CHAPTER TWELVE
`MENUS AND KEYBOARD ACCELERATORS .............. 215
`CHAPTER THIRTEEN
`TOOLBARS AND STATUS BARS. . . . . . . . . . . . . . . . . . . . . . .. 237
`
`4
`
`
`
`INSIDE VISUAL C++
`
`CHAPTER FOURTEEN
`A REUSABLE BASE CLASS . . . . . . . . . . . . . . . . . . . . . . . . . .. 257
`CHAPTER FIFTEEN
`SEPARATING THE DOCUMENT FROM ITS VIEW. . . . . . . . . .. 273
`CHAPTER SIXTEEN
`READING AND WRITING DOCUMENTS-SOl ............. 317
`CHAPTER SEVENTEEN
`READING AND WRITING DOCUMENTS-MOl ............ 347
`CHAPTER EIGHTEEN
`PRINTING AND PRINT PREVIEW. . . . . . . . . . . . . . . . . . . . . .. 371
`CHAPTER NINETEEN
`SPLITTER WINDOWS AND MULTIPLE VIEWS. . . . . . . . . . . .. 391
`CHAPTER TWENTY
`CONTEXT-SENSITIVE HELP ................. '. . . . . . . . .. 403
`CHAPTER TWENTY·ONE
`A PRACTICAL WINDOWS-BASED APPLICATION . . . . . . . . .. 425
`
`PART IV ADVANCED TOPICS ........................... 451
`CHAPTER TWENTY·TWO
`MFC LIBRARY VERSION 2.5 PROGRAMS
`WITHOUT DOCUMENTS OR VIEWS. . . . . . . . . . . . . . . . . . . .. 453
`CHAPTER TWENTY· THREE
`STORING BITMAPS IN A DOCUMENT-
`DIBS AND THE CLIPBOARD .......................... 473
`CHAPTER TWENTY·FOUR
`DATABASE MANAGEMENT WITH MICROSOFT ODBC . . . . .. 501
`CHAPTER TWENTY·FIVE
`OLE AND OLE AUTOMATION. . . . . . . . . . . . . . . . . . . . . . . . .. 549
`CHAPTER TWENTY·SIX
`DYNAMIC LINK LIBRARIES (DLLs) ..................... 635
`
`PART V APPENDiXES.................................. 655
`APPENDIX A
`A CRASH COURSE IN THE C++ LANGUAGE. . . . . . . . . . . .. 657
`APPENDIX B
`MESSAGE MAP FUNCTIONS IN THE
`MICROSOFT FOUNDATION CLASS LIBRARY. . . . . . . . . . . .. 693
`APPENDIXC
`MICROSOFTWINDOWS FUNCTIONS USED IN THIS BOOK .. 699
`APPENDIX D
`VISUAL C++, VERSION 2.0 FOR WINDOWS NT . . . . . . . . . .. 701
`
`INDEX ........................................................ 707
`
`iv
`
`5
`
`
`
`TABLE OF CONTENTS
`
`ACKNOWLEDGMENTS .......................... . . . . . . . . . . . . . . . . xxiii
`INTRODUCTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. xxv
`
`PART I WINDOWS, VISUAL C++, AND APPLICATION
`FRAMEWORK FUNDAMENTALS . . . . . . . . . . . . . . . . . . . .. 1
`CHAPTER ONE
`MICROSOFT WINDOWS AND VISUAL C++. . . . . . . . . . . . . . . .. 3
`The Windows Programming Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
`Message Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 3
`The Windows Graphics Device Interface (GDI) .................... , 4
`Resource-Based Programming ................................. 5
`Memory Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 5
`Dynamic Link Libraries (DLLs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 5
`Windows NT ................................................ 6
`The Visual C++ Components ................................. 6
`Visual Workbench and the Build Process ......................... , 7
`The App Studio Resource Editor ................................ 9
`The C/C++ Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 10
`The Linker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 10
`The Resource Compiler ...................................... 10
`The Debugger. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 10
`AppWizard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 11
`ClassWizard ............................................... 12
`The Source Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 12
`Online Help ................................................ 13
`Windows Diagnostic Tools .................................... 13
`The Microsoft Foundation Class Library Version 2.5 . . . . . . . . . . . . . . .. 14
`CHAPTER TWO
`THE MICROSOFT FOUNDATION CLASS LIBRARY
`APPLICATION FRAMEWORK . . . . . . . . . . . . . . . . . . . . . . . . . .. 15
`Why Use the Application Framework? ......................... 15
`The Learning Curve. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 18
`What's an Application Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . .. 19
`The Application Framework vs. the MFC Library. . . . . . . . . . . . . . . . . .. 19
`An Application Framework Example. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 19
`MFC Library Message Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 23
`Documents and Views ....................................... 23
`
`6
`
`
`
`INSIDE VISUAL C++
`
`PART II THE MFC LIBRARY VIEW CLASS ................... 27
`CHAPTER THREE
`GETTING STARTED WITH APPWIZARD-"HELLO, WORLD!" .. 29
`What's a View? ............................................ 30
`Single Document Interface (SDI) vs.
`Multiple Document Interface (MDI) .......................... 30
`The "Do-Nothing" Application ................................ 31
`The CEx03aViewView Class ................................. 34
`Drawing Inside the View Window-The Windows
`Graphics Device Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 35
`The OnOraw Member Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 35
`The Windows Device Context. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 35
`Adding Draw Code to the EX03A Program .......... ;............ 36
`A Preview of App Studio-Resources Introduced ....... : . . . . . . . .. 37
`The Contents of EX03A.RC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 37
`Running App Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 38
`The Windows Debug Kernel and DBWIN ....................... 40
`Do You Need to Use the Debugger? . . . . . . . . . . . . . . . . . . . . . . . . . .. 42
`Enabling the Diagnostic Macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 43
`Speeding Up the Build Process .............................. 43
`Creating a New MAK File ................................... 50
`CHAPTER FOUR
`BASIC EVENT HANDLING-USING CLASSWIZARD . . . . . . . .. 51
`Getting User Input-Message Map Functions ................... 51
`The Message Map .......................................... 52
`Saving the View's State-Class Data Members ..................... 52
`Initializing a View Class Data Member. . . . . . . . . . . . . . . . . . . . . . . . . .. 53
`Invalidating the Rectangle .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 53
`The Window's Client Area. . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 54
`The EX04A Example Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 54
`Using ClassWizard with EX04A .............................. , 57
`Using AppWizard and ClassWizard Together . . . . . . . . . . . . . . . . . . . .. 58
`EX04B-Dragging a Circle with the Mouse. . . . . . . . . . . . . . . . . . . . .. 60
`The EX04B Program Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 63
`The m_el/ipseRect Data Member. . . . . . . . . . . . . . . . . . . . . . . . .. 63
`The m_mousePos Data Member . . . . . . . . . . . . . . . . . . . . . . . . .. 63
`The m_bGaptured Data Member. . . . . . . . . . . . . . . . . . . . . . . . .. 63
`The SetGapture and ReieaseGapture Member Functions . . . . . .. 64
`The SetGursorand LoadGursorWindows Functions ........... 64
`GRect, GPoint, and GSize Arithmetic . . . . . . . . . . . . . . . . . . . . . .. 64
`Is a Point Inside the Client Area? . . . . . . . . . . . . . . . . . . . . . . . . .. 65
`Is a Point Inside a Circle? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 65
`The Minimum Invalid Rectangle. . . . . . . . . . . . . . . . . . . . . . . . ... 65
`The GRect::LPREGTOperator ............................ 66
`Device Coordinates-Necessary for This Example. . . . . . . . . . .. 66
`
`vi
`
`7
`
`
`
`Table of Contents
`
`A Scrolling View Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 66
`A Window Is Larger than What You See. . . . . . . . . . . . . . . . . . . . . . . . .. 66
`Scroll Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 67
`Scrolling Alternatives ........................................ 67
`The EX04C Scrolling Example ................................. 67
`The EX04C Program Elements . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . .. 70
`The Windows GetSystemMetrics Function. . . . . . . . . . . . . . . . . .. 70
`The Virtual OnlnitialUpdate Function . . . . . . . . . . . . . . . . . . . . . .. 70
`The SetScrol/Sizes Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 71
`Handling Keystrokes ................................... 71
`Connecting Scroll Keys to CScrol/View . . . . . . . . . . . . . . . . . . . .. 71
`Coordinate Transformations-Not Yet ...................... 71
`Other Windows Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 72
`The WM_CREATE Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 72
`The WM_CLOSE Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 72
`The WM_QUERYENDSESSION Message ........................ 72
`The WM_DESTROY Message. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 73
`The WM_NCDESTROY Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 73
`CHAPTER FIVE
`THE GRAPHICS DEVICE INTERFACE (GOI). . . . . . . . . . . . . . .. 75
`The Device Context Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 75
`The Display Context Classes CClientDC and CWindowDC . . . . . . . . .. 76
`Constructing and Destroying CDC Objects. . . . . . . . . . . . . . . . . . . . . .. 76
`The State of the Device Context. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 77
`The CPaintDC Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 78
`GDI Objects .............................................. 78
`Constructing and Destroying GDI Objects. . . . . . . . . . . . . . . . . . . . . . .. 79
`Tracking GDI Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 79
`Stock GDI Objects .......................................... 80
`The Lifetime of a GDI Selection ................................ 81
`A Permanent Device Context for the Display-
`Registering Window Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 82
`Windows Color Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 82
`Standard Video Graphics Array (VGA) Display Boards. . . . . . . . . . . . .. 83
`256-Color Display Boards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 84
`24-Bit Color Display Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 85
`Mapping Modes .......................................... 85
`The MM_ TEXT Mapping Mode ........................... '. . . . .. 85
`The "Fixed Scale" Mapping Modes ......... -. . . . . . . . . . . . . . . . . . .. 86
`The "Variable Scale" Mapping Modes . . . . . . . . . . . . . . . . . . . . . . . . . .. 87
`Coordinate Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 89
`Fonts ................................................... 90
`Fonts Are GDI Objects ....................................... 91
`Choosing a Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 91
`Printing with Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 91
`Displaying Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 91
`
`vii
`
`8
`
`
`
`INSIDE VISUAL C++
`
`Points in a Window-Logical Twips ............................. 92
`Computing Character Height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 93
`The EX05A Program ....................................... 94
`The EX05A Program Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 97
`Mapping Mode Set in the OnPrepareOC Function ........... " ~7
`The ShowFont Private Member Function . . . . . . . . . . . . . . . . . . .. 97
`The Call to CFont::CreateFont ............................ 97
`The EX05B Program ........................................ 98
`The EX05C Example-CScrol/View Revisited . . . . . . . . . . . . . . . . . .. 102
`The EX05C Program Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 105
`The C$croIiView::OnPrepareOC Member Function ........... 105
`The OnMouseMove Coordinate Transformation Code ......... 106
`The OnOraw Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 106
`The CScroIlView::SetScale ToFitSize Mode. . . . . . . . . . . . . . . . . . . . . .. 106
`CHAPTER SIX
`THE MODAL DIALOG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 107
`Modal vs. Modeless Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 107
`System Modal Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 108
`Resources and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 108
`Programming a Modal Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 109
`The Dialog That Ate Cincinnati-The EX06A Example . . . . . . . . . . .. 110
`ClassWizard and the Dialog Class .................... .'. . . . . . .. 118
`Connecting the Dialog to the View. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 122
`Understanding the EX06A Application. . . . . . . . . . . . . . . . . . . . . . . . .. 124
`Enhancing the Dialog Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 125
`Taking Control of the OnOK Exit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 125
`On Cancel Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 127
`Hooking Up the Scroll Bar Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .. 127
`Identifying Controls: CWnd Pointers and Control IDs ............. 130
`Setting the Color for the Dialog Background and for Controls ...... 131
`Painting Inside the Dialog Window ........................... 131
`Using Other Control Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 132
`CHAPTER SEVEN
`THE MODELESS DIALOG AND
`THE COMMDLG DIALOG CLASSES .................... 133
`Modeless Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 133
`Creating Modeless Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 133
`User-Defined Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 134
`Dialog Ownership .......................................... 134
`A Modeless Dialog Example-EX07 A . . . . . . . . . . . . . . . . . . . . . . . . .. 134
`The CFormViewClass-A Modeless Dialog Alternative ........... 141
`COMMDLG Dialogs ....................................... 141
`Using the CFileOialog Class Directly . . . . . . . . . . . . . . . . . . . . . . . . . .. 142
`Adding Dialog Controls at Run Time .. . . . . . . . . . . . . . . . . . . . . . . . .. 142
`
`viii
`
`9
`
`
`
`Table of Contents
`
`Deriving from the COMMDLG Classes .......................... 143
`A CFileOialog Example-EX07B ............................... 143
`CHAPTER EIGHT
`VISUAL BASIC CONTROLS . . . . . . . . . . . . . . . . . . . . . . . . . .. 149
`Standard Controls for Windows
`and Ordinary Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . .. 150
`C++ Classes and Visual Basic Controls . . . . . . . . . . . . . . . . . . . . . .. 150
`Methods vs. Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 150
`Properties vs. Data Members ................................. 151
`Visual Basic Control Events vs. Windows Control Notifications. . . . . . .. 151
`Visual Basic Event Registration .............................. 152
`The GVBContro/ Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 152
`The EX08A Example ...................................... 153
`The Visual Basic GRID Control ................................ 153
`Building the EX08A Example Program ......................... , 155
`Using Other Visual Basic Controls. . . . . . . . . . . . . . . . . . . . . . . . . . .. 164
`Visual Basic Picture Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 164
`Advantages and Disadvantages of
`Writing and Using Visual Basic Controls. . . . . . . . . . . . . . . . . . . .. 164
`CHAPTER NINE
`WINDOWS MEMORY MANAGEMENT-JUST SAY "NEW" ... 167
`A Memory Model Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 168
`16-Bit Windows ........................................... 169
`The Intel Segment Architecture ............................... 169
`The DGROUP Segment and Multi-Instance Programs ............. 170
`Memory Models-One Code Segment or Many? ................. 171
`Near Function Calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 171
`Memory Models-One Data Segment or Many? . . . . . . . . . . . . . . . . .. 173
`The Local Heap ............................................ 174
`The Global Heap ........................................... 175
`Location of the Program's vtbl ................................ 176
`Direct Use of Windows Memory Allocation Functions. . . . . . . . . . . . .. 177
`The 80386/80486 Virtual Memory Manager. . . . . . . . . . . . . . . . . . . . .. 177
`Detecting Memory Leaks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 178
`What to Do When You Run Out of Memory . . . . . . . . . . . . . . . . . . . .. 179
`CHAPTER TEN
`BITMAPS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 181
`GDI Bitmaps and Device-Independent Bitmaps (DIBs) ........... 181
`Using GDI Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 182
`Color Bitmaps and Monochrome Bitmaps ..................... 182
`Loading a GDI Bitmap from a Resource. . . . . . . . . . . . . . . . . . . . . .. 183
`The Effect of the Display Mapping Mode . . . . . . . . . . . . . . . . . . . . .. 184
`Stretching the Bits ................................. : . . . . .. 184
`The EX10A Program ........................................ 184
`
`ix
`
`10
`
`
`
`INSIDE VISUAL C++
`
`Using Bitmaps to Improve the Screen Display. . . . . . . . . . . . . . . . .. 187
`The EX 1 OB Prog ra:m ...................................... " 188
`Other Uses for GDI Bitmaps .............................. " 192
`CHAPTER ELEVEN
`BITMAP BUTTONS, THE TIMER, AND
`ON-IDLE PROCESSING ............................. , 195
`Bitmap Buttons .......................................... 195
`The EX11AProgram ........................................ 195
`Going Further with Bitmap Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 201
`Using a Timer and Yielding Control. . . . . . . . . . . . . . . . . . . . . . . . . .. 201
`Timers ................................................. " 201
`Yielding Control .......................................... " 202
`The EX11 B Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 203
`On-Idle Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 206
`The EX11C Program ...................................... " 207
`
`PART III THE DOCUMENT-VIEW ARCHITECTURE ........... 213
`CHAPTER TWELVE
`MENUS AND KEYBOARD ACCELERATORS . . . . . . . . . . . . .. 215
`The Main Frame Window and Document Classes ............... 215
`Windows Menus ......................................... 216
`Keyboard Accelerators .................................. " 217
`Command Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 218
`Command Message Handling in Derived Classes ................ 219
`Update Command User Interface (UI) Messages ............... " 219
`Commands That Originate in Dialogs .......................... 220
`The Application Framework's Built-In Menu Items ............... 221
`Enabling/Disabling of Menu Items. . . . . . . . . . . . . . . . . . . . . . . . . . .. 222
`The CEditView Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 222
`The EX12A Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 223
`The CMenu Class ........................................ 229
`Extended Command Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 230
`The EX12B Example ...................................... 231
`CHAPTER THIRTEEN
`TOOLBARS AND STATUS BARS. . . . . . . . . . . . . . . . . . . . . . .. 237
`Control Bars and the Application Framework. . . . . . . . . . . . . . . . . .. 237
`The Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 238
`The Toolbar Bitmap ....................................... " 238
`Button States. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 239
`The Toolbar and Command Messages. . . . . . . . . . . . . . . . . . . . . . . . .. 239
`Toolbar Update Command UI Messages ...................... " 240
`Locating the Main Frame Window .......................... " 241
`The EX13A Toolbar Example ................................ 242
`
`x
`
`11
`
`
`
`Table of Contents
`
`The Status Bar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 248
`The Status Bar Definition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 248
`The Message Line. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 248
`The Status Indicator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 249
`Taking Control of the Status Bar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 249
`The EX13B Status Bar Example ............................. 250
`CHAPTER FOURTEEN
`A REUSABLE BASE CLASS . . . . . . . . . . . . . . . . . . . . . . . . . .. 257
`Why Reusable Base Classes Are Difficult to Write . . . . . . . . . . . . . .. 257
`The CPersistentFrame Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 258
`The CFrameWnd Class and the ActivateFrame Member Function. .. 258
`The Windows INI File ...................................... 259
`Using the CString Class .................................. .261
`The Position of a Maximized Window . . . . . . . . . . . . . . . . . . . . . . . .. 263
`Static Data Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 263
`The Default Window Rectangle. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 264
`The EX14A Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 264
`Persistent Frames in MOl Applications. . . . . . . . . . . . . . . . . . . . . . .. 270
`CHAPTER FIFTEEN
`SEPARATING THE DOCUMENT FROM ITS VIEW ........... 273
`Document-View Interaction Functions ........................ 273
`The CView::GetDocument Function . . . . . . . . . . . . . . . . . . . . . . . . . . .. 274
`The COocument::UpdateAIIViews Function . . . . . . . . . . . . . . . . . . . . .. 274
`The CView::OnUpdate Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 275
`The CView::OnlnitiaIUpdate Function. . . . • . . . . . . . . . . . . . . . . . . . . .. 275
`The Simplest Document-View Application . . . . . . . . . . . . . . . . . . . .. 276
`The CForm View Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 277
`The CObject Class ....................................... 278
`Diagnostic Dumping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 278
`The TRACE Macro. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 278
`The atxOump Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 279
`The Dump Context and the CObject Class . . . . . . . . . . . . . . . . . . . . .. 279
`Automatic Dump of Undeleted Objects. . . . . . . . . . . . . . . . . . . . . . . .. 281
`The EX15A Example ...................................... 282
`A More Advanced Document-View Interaction .................. 290
`The COocument::OeleteContents Function. . . . . . . . . . . . . . . . . . . .. 291
`The CObList Collection Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 291
`Using the CObList Class for a First-In, First-Out (FIFO) List ......... 292
`CObList Iteration-The POSITION Variable. . . . . . . . . . . . . . . . . . . . .. 294
`The Dump Context and Collection Classes . . . . . . . . . . . . . . . . . . . . .. 295
`The EX15B Example ...................................... 296
`CEx15bApp . ............................................ 298
`CMainFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 298
`
`xi
`
`12
`
`
`
`INSIDE VISUAL C++
`
`CStudentOoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . .. 298
`ClassWizard and CStudentDoc ............................. " 302
`Data Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 302
`Constructor and Destructor .................................. 302
`GetList ................................................. " 30,2
`DeleteContents .......................................... " 302
`Dump .................................................... 302
`CStudentView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 303
`ClassWizard and CStudentView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 311
`Data Members ............................................. 312
`OnlnitialUpdate . ........................................... 312
`OnUpdate .............................................. " 312
`Toolbar Button Command Message Handlers .................. " 312
`Toolbar Button Update Command UI Message Handlers ........... 312
`Protected Virtual Functions ................................... 313
`Resource Requirements ................................... 313
`Symbols ................................................ " 313
`Edit Menu ................................................ 314
`The IDD_STUDENT Dialog ................................. " 314
`Toolbar ................................................... 314
`Testing the EX15B Application ................................ 315
`Two Exercises for the Reader .............................. , 316
`CHAPTER SIXTEEN
`READING AND WRITING DOCUMENTS-SOl ............. 317
`Serialization-What Is It? .................................. , 317
`Disk Files and Archives ...................................... 318
`Making a Class Serializable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 318
`Writing a Serialize Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 319
`Loading from an Archive-Embedded Objects vs. Pointers. . . . . . . .. 320
`Serializing Collections ........ '.' ........................... " 322
`The Serialize Function and the Application Framework. . . . . . . . . . . .. 323
`The SOl Application ....................................... 324
`The Windows Application Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 324
`The Document Template Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 325
`The Document Template Resource ........................... " 327
`Multiple Views of an SOl Document .......................... " 328
`Creating an Empty Document-
`The CWinApp::OnFileNew Function ......................... 328
`The Document Class's OnNewDocument Function .............. " 329
`Connecting File Open
`to Your Serialization Code-OnFileOpen. . . . . . . . . . . . . . . . . . . . .. 329
`The Document Class's DeleteContents Function. . . . . . . . . . . . . . . . .. 330
`Connecting File Save and File Save As
`to Your Serialization Code .... . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 330
`The Document's IsModified Flag .............................. 330
`
`xii
`
`13
`
`
`
`Table of Contents
`
`EX16A-An SDI Example with Serialization .................... 331
`CStudent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 332
`CEx16aApp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 334
`CFrameWnd. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .