Introduction...... 1 What's New in the Second Edition ...... 2 What's On the CD ...... 4 From Me to You (and You to Me) ...... 4 PART Ⅰ: Fundamental of Windows and MFC ...... 6 Chapter 1. Hello, MFC...... 6 1.1. The Windows Programming Model ...... 7 1.1.1. Messages, Messages, and More Messages...... 9 1.1.2. Windows Programming, SDK-Style...... 11 1.1.3. Hungarian Notation and Windows Data Types...... 16 1.1.4. SDK Programming in Perspective ...... 17 1.2. Introducing MFC...... 18 1.2.1. The Benefits of Using C++ and MFC...... 19 1.2.2. The MFC Design Philosophy...... 20 1.2.3. The Document/View Architecture ...... 21 1.2.4. The MFC Class Hierarchy ...... 22 1.2.5. AFX Functions...... 23 1.3. Your First MFC Application...... 23 1.3.1. The Application Object...... 26 1.3.2. How MFC Uses the Application Object ...... 29 1.3.3. The Frame Window Object...... 31 1.3.4. Painting the Window...... 34 1.3.5. The Message Map...... 37 1.3.6. How Message Maps Work...... 39 1.3.7. Windows, Character Sets, and the _T Macro...... 42 1.3.8. Building the Application...... 44 1.3.9. The Big Picture ...... 45 Chapter 2. Drawing in a Window ...... 48 2.1. The Windows GDI ...... 49 2.1.2. The MFC Device Context Classes ...... 50 2.1.3. Device Context Attributes...... 54 2.1.4. The Drawing Mode...... 56 2.1.5. The Mapping Mode...... 58 2.2. Drawing with the GDI...... 68 2.2.1. Drawing Lines and Curves...... 69 2.2.2. Drawing Ellipses, Polygons, and Other Shapes ...... 73 2.2.3. GDI Pens and the CPen Class ...... 76 2.2.4. GDI Brushes and the CBrush Class ...... 81 2.2.5. Drawing Text...... 85 2.2.6. GDI Fonts and the CFont Class ...... 88 2.2.7. Raster Fonts vs. TrueType Fonts...... 91 2.2.8. Rotated Text ...... 91 2.2.9. Stock Objects ...... 93 2.2.10. Deleting GDI Objects...... 95 2.2.11. Deselecting GDI Objects ...... 96 2.2.12. The Ruler Application...... 98 2.3. Seeing What You've Drawn...... 101 2.3.1. Adding a Scroll Bar to a Window ...... 102 2.3.2. Setting a Scroll Bar's Range, Position, and Page Size...... 103 2.3.3. Synchronizing the Thumb Size and the Window Size ...... 106 2.3.4. Processing Scroll Bar Messages...... 107 2.3.5. Scrolling a Window...... 109 1 2.3.6. The Accel Application ...... 111 2.4. Loose Ends ...... 120 Chapter 3. The Mouse and the Keyboard...... 122 3.1. Getting Input from the Mouse ...... 123 3.1.1. More About the TicTac Window...... 146 3.1.2. The PostNcDestroy Function...... 148 3.1.3. Nonclient-Area Mouse Messages...... 149 3.1.4. The WM_NCHITTEST Message ...... 152 3.1.5. The WM_MOUSELEAVE and WM_MOUSEHOVER Messages ...... 153 3.2. Getting Input from the Keyboard...... 173 3.2.1. The Input Focus...... 173 3.2.2. Keystroke Messages ...... 175 3.2.3. Virtual Key Codes ...... 178 3.2.4. Shift States and Toggles...... 180 3.2.5. Character Messages...... 182 3.2.6. Dead-Key Messages ...... 185 3.2.7. The Caret ...... 186 3.3. The VisualKB Application...... 190 3.3.1. Handling the Caret...... 202 3.3.2. Entering and Editing Text...... 206 3.3.3. Other Points of Interest...... 207 Chapter 4. Menus ...... 209 4.1. Menu Basics ...... 210 4.1.1. Creating a Menu ...... 211 4.1.2. Loading and Displaying a Menu ...... 214 4.1.3. Responding to Menu Commands ...... 217 4.1.4. Command Ranges...... 218 4.1.5. Updating the Items in a Menu ...... 220 4.1.6. Update Ranges...... 224 4.1.7. Keyboard Accelerators ...... 225 4.2. The Shapes Application ...... 228 4.2.1. Running the MFC AppWizard...... 245 4.2.2. Analyzing AppWizard's Output...... 248 4.2.3. Beyond AppWizard ...... 252 4.2.4. The Process in Review ...... 258 4.3. Menu Magic ...... 259 4.3.1. Creating Menus Programmatically...... 259 4.3.2. Modifying Menus Programmatically...... 260 4.3.3. The System Menu...... 262 4.3.4. Owner-Draw Menus ...... 265 4.3.5. OnMenuChar Processing...... 269 4.3.6. Cascading Menus...... 271 4.3.7. Context Menus ...... 272 4.3.8. The TPM_RETURNCMD Flag...... 276 4.4. The Colors Application ...... 277 4.4.1. The Context Menu...... 302 4.4.2. On Your Own...... 304 Chapter 5. The MFC Collection Classes ...... 307 5.1. Arrays...... 307 5.1.1. The MFC Array Classes ...... 308 5.1.2. Dynamic Array Sizing ...... 312 5.1.3. Creating Type-Safe Array Classes with CArray ...... 315 2 5.2. Lists...... 317 5.2.1. The MFC List Classes...... 318 5.2.2. Creating Type-Safe List Classes with CList...... 322 5.3. Maps...... 324 5.3.1. The MFC Map Classes...... 324 5.3.2. How Maps Work ...... 326 5.3.3. Optimizing Lookup Efficiency ...... 328 5.3.4. Creating Type-Safe Map Classes with CMap ...... 330 5.4. The Typed Pointer Classes ...... 331 Chapter 6. File I/O and Serialization ...... 335 6.1. The CFile Class ...... 335 6.1.1. Opening, Closing, and Creating Files ...... 336 6.1.2. Reading and Writing ...... 339 6.1.3. CFile Derivatives ...... 341 6.1.4. Enumerating Files and Folders...... 343 6.2. Serialization and the CArchive Class ...... 346 6.2.1. Serialization Basics...... 347 6.2.2. Writing Serializable Classes...... 349 6.2.3. Versioning Serializable Classes: Versionable Schemas...... 351 6.2.4. How Serialization Works ...... 354 6.2.5. Serializing CObjects ...... 360 Chapter 7. Controls ...... 363 7.1. The Classic Controls...... 364 7.1.1. The CButton Class ...... 367 7.1.2. The CListBox Class...... 373 7.1.3. The CStatic Class ...... 382 7.1.4. The FontView Application...... 386 7.1.5. The CEdit Class ...... 395 7.1.6. The CComboBox Class ...... 411 7.1.7. The CScrollBar Class...... 416 7.2. Advanced Control Programming ...... 417 7.2.1. Numeric Edit Controls ...... 418 7.2.2. Owner-Draw List Boxes ...... 419 7.2.3. Graphical Push Buttons...... 430 7.2.4. Customizing a Control's Colors ...... 431 7.2.5. Message Reflection...... 439 Chapter 8. Dialog Boxes and Property Sheets...... 443 8.1. Modal Dialog Boxes and the CDialog Class...... 444 8.1.1. The Dialog Box Template ...... 444 8.1.2. The CDialog Class ...... 452 8.1.3. Creating a Modal Dialog Box ...... 456 8.1.4. Dialog Data Exchange and Dialog Data Validation ...... 458 8.1.5. Interacting with the Controls in a Dialog...... 466 8.1.6. The DlgDemo1 Application...... 468 8.2. Modeless Dialog Boxes ...... 480 8.2.1. The DlgDemo2 Application...... 480 8.3. Using a Dialog Box as a Main Window...... 494 8.3.1. Processing Keyboard Messages ...... 507 8.3.2. Preprocessing WM_COMMAND Messages ...... 509 8.4. Property Sheets ...... 510 8.4.1. The PropDemo Application ...... 514
3 8.5. The Common Dialogs...... 529 8.5.1. Modifying the Common Dialogs...... 531 8.5.2. The Phones Application...... 533 PART Ⅱ: The Documents/View Architecture...... 549 Chapter 9. Documents, Views, and the Single Document Interface .549 9.1. Document/View Fundamentals ...... 550 9.1.1. The InitInstance Function Revisited...... 552 9.1.2. The Document Object...... 554 9.1.3. The View Object...... 560 9.1.4. The Frame Window Object...... 564 9.1.5. Dynamic Object Creation ...... 564 9.1.6. More on the SDI Document Template...... 566 9.1.7. Registering Document Types with the Operating System Shell ...... 568 9.1.8. Command Routing...... 569 9.1.9. Predefined Command IDs and Command Handlers...... 572 9.2. Your First Document/View Application ...... 575 9.2.1. The SdiSquares Application ...... 575 9.2.2. SdiSquares Step by Step ...... 595 9.3. Doc + View = Less Work for You ...... 597 Chapter 10. Scroll Views, HTML Views, and Other View Types...... 599 10.1. Scroll Views ...... 600 10.1.1. CScrollView Basics...... 600 10.1.2. CScrollView Operations...... 605 10.1.3. Optimizing Scrolling Performance...... 605 10.1.4. The ScrollDemo Application...... 607 10.1.5. Converting an Ordinary View into a Scroll View...... 615 10.2. HTML Views...... 615 10.2.1. CHtmlView Operations ...... 616 10.2.2. CHtmlView Overridables ...... 618 10.2.3. Utilizing DHTML in CHtmlView-Based Applications ...... 620 10.3. Tree Views ...... 626 10.3.1. Initializing a Tree View ...... 627 10.3.2. Tree View Member Functions and Notifications...... 630 10.3.3. The DriveTree Application...... 632 10.4. List Views ...... 644 10.4.1. Initializing a List View ...... 645 10.4.2. Changing the Presentation Style...... 648 10.4.3. Sorting in a List View...... 649 10.4.4. Hit-Testing in a List View...... 650 10.4.5. The WinDir Application ...... 651 10.5. Do-It-Yourself Control Views ...... 665 Chapter 11. Multiple Documents and Multiple Views...... 669 11.1. MFC and the Multiple Document Interface...... 669 11.1.1. Synchronizing Multiple Views of a Document...... 672 11.1.2. The MdiSquares Application...... 675 11.1.3. Supporting Multiple Document Types...... 694 11.1.4. Alternatives to MDI...... 695 11.2. Splitter Windows ...... 696 11.2.1. Dynamic Splitter Windows...... 698 11.2.2. The Sketch Application ...... 700 11.2.3. Static Splitter Windows ...... 718 4 11.2.4. The Wanderer Application ...... 719 11.2.5. Custom Command Routing...... 749 11.2.6. Three-Way Splitter Windows...... 751 11.2.7. Dynamic Splitter Windows with Multiple View Types...... 753 Chapter 12. Toolbars, Status Bars, and Rebars ...... 755 12.1. Toolbars ...... 755 12.1.1. Creating and Initializing a Toolbar...... 756 12.1.2. Docking and Floating...... 762 12.1.3. Controlling a Toolbar's Visibility ...... 766 12.1.4. Keeping Toolbar Buttons in Sync with Your Application ...... 768 12.1.5. Adding ToolTips and Flyby Text ...... 770 12.1.6. Adding Non-Push-Button Controls to a Toolbar...... 774 12.1.7. Updating Non-Push-Button Controls...... 775 12.1.8. Making Toolbar Settings Persistent...... 777 12.1.9. Toolbar Support in AppWizard ...... 777 12.2. Status Bars...... 778 12.2.1. Creating and Initializing a Status Bar ...... 779 12.2.2. Providing Context-Sensitive Help for Menu Items...... 782 12.2.3. Creating Custom Status Bar Panes...... 783 12.2.4. Status Bar Support in AppWizard ...... 786 12.3. Putting It All Together: The MyWord Application...... 787 12.3.1. The Main Toolbar...... 807 12.3.2. The Style Bar ...... 808 12.3.3. More About CRichEditView...... 813 12.4. Rebars ...... 814 Chapter 13. Printing and Print Previewing ...... 817 13.1. Printing with Documents and Views ...... 817 13.1.1. The Windows Print Architecture...... 818 13.1.2. The MFC Print Architecture ...... 825 13.1.3. Print Previewing...... 834 13.2. A Bare-Bones Printing Application ...... 835 13.2.1. Black-and-White Print Previews...... 841 13.3. A More Complex Printing Application ...... 842 13.3.1. A Unique Approach to Serialization ...... 856 13.4. Printing Tips and Tricks...... 857 13.4.1. Using the Print Dialog's Selection Button...... 857 13.4.2. Assume Nothing—And Test Thoroughly! ...... 858 13.4.3. Adding Default Pagination Support...... 861 13.4.4. Enumerating Printers...... 862 PART Ⅲ: Beyong the Basics ...... 865 Chapter 14. Timers and Idle Processing ...... 865 14.1. Timer...... 865 14.1.1. Setting a Timer: Method 1 ...... 866 14.1.2. Responding to WM_TIMER Messages ...... 869 14.1.3. Setting a Timer: Method 2 ...... 872 14.1.4. Stopping a Timer...... 874 14.2. The Clock Application ...... 875 14.2.1. Processing Timer Messages ...... 885 14.2.2. Getting the Current Time:The CTime Class ...... 887 14.2.3. Using the MM_ISOTROPIC Mapping Mode...... 888 14.2.4. Hiding and Displaying the Title Bar ...... 890 5 14.2.5. Implementing Client-Area Drag...... 892 14.2.6. Using the System Menu as a Context Menu...... 893 14.2.7. Topmost Windows ...... 895 14.2.8. Making Configuration Settings Persistent...... 896 14.2.9. Controlling the Window Size: The WM_GETMINMAXINFO Message 903 14.3. Idle Processing ...... 904 14.3.1. Using OnIdle ...... 906 14.3.2. Idle Processing vs. Multithreading ...... 909 Chapter 15. Bitmaps, Palettes, and Regions ...... 911 15.1. Palettes...... 912 15.1.1. How Windows Uses Color ...... 912 15.1.2. Logical Palettes and the CPalette Class...... 914 15.1.3. Creating a Logical Palette...... 915 15.1.4. Realizing a Logical Palette...... 919 15.1.5. Drawing with Palette Colors...... 920 15.1.6. The WM_QUERYNEWPALETTE and WM_PALETTECHANGED Msg 921 15.1.7. Determining Whether a Logical Palette Is Needed...... 924 15.1.8. The PaletteDemo Application...... 926 15.1.9. Palette Animation ...... 932 15.1.10. The ::SetSystemPaletteUse Function ...... 937 15.2. Bitmaps...... 938 15.2.1. DDBs and the CBitmap Class...... 938 15.2.2. Blitting Bitmaps to Screens and Other Devices...... 940 15.2.3. Bitmap Resources...... 943 15.2.4. DIBs and DIB Sections ...... 945 15.2.5. Blits, Raster Operations, and Color Mapping...... 947 15.2.6. The BitmapDemo Application...... 950 15.2.7. Writing a BMP File Viewer ...... 965 15.2.8. More on the ::LoadImage Function...... 981 15.3. Regions ...... 982 15.3.1. Regions and the CRgn Class...... 983 15.3.2. The RegionDemo Application...... 988 Chapter 16. The Common Controls ...... 993 16.1. Common Control Fundamentals...... 994 16.1.1. Creating a Common Control...... 997 16.1.2. Processing Notifications: The WM_NOTIFY Message ...... 1000 16.2. Slider, Spin Button, and ToolTip Controls ...... 1004 16.2.1. Slider Controls...... 1004 16.2.2. Spin Button Controls ...... 1009 16.2.3. ToolTip Controls...... 1013 16.2.4. The GridDemo Application ...... 1018 16.3. Image Lists and ComboBoxEx Controls ...... 1028 16.3.1. Image Lists ...... 1029 16.3.2. ComboBoxEx Controls ...... 1032 16.3.3. The PathList Application...... 1036 16.4. Progress Controls and Animation Controls ...... 1045 16.4.1. Progress Controls...... 1046 16.4.2. Animation Controls ...... 1048 16.5. IP Address Controls and Other Data-Entry Controls...... 1050 16.5.1. IP Address Controls...... 1050 16.5.2. Hotkey Controls...... 1052 6 16.5.3. Month Calendar Controls...... 1053 16.5.4. Date-Time Picker Controls ...... 1057 Chapter 17. Threads and Thread Synchronization...... 1061 17.1. Threads ...... 1062 17.1.1. Creating a Worker Thread ...... 1063 17.1.2. Creating a UI Thread...... 1066 17.1.3. Suspending and Resuming Threads ...... 1067 17.1.4. Putting Threads to Sleep ...... 1068 17.1.5. Terminating a Thread ...... 1069 17.1.6. Autodeleting CWinThreads...... 1070 17.1.7. Terminating Another Thread ...... 1072 17.1.8. Threads, Processes, and Priorities...... 1075 17.1.9. Using C Run-Time Functions in Multithreaded Applications...... 1080 17.1.10. Calling MFC Member Functions Across Thread Boundaries...... 1081 17.1.11. Your First Multithreaded Application ...... 1085 17.2. Thread Synchronization...... 1093 17.2.1. Critical Sections...... 1094 17.2.2. Mutexes...... 1096 17.2.3. Events...... 1098 17.2.4. Semaphores...... 1102 17.2.5. The CSingleLock and CMultiLock Classes ...... 1104 17.2.6. Writing Thread-Safe Classes...... 1107 17.2.7. The ImageEdit Application ...... 1109 17.3. Odds and Ends ...... 1132 17.3.1. Message Pumps...... 1132 17.3.2. Launching Other Processes...... 1134 17.3.3. File Change Notifications ...... 1136 PART Ⅳ: COM, OLE and ActiveX...... 1139 Chapter 18. MFC and the Component Object Model...... 1139 18.1. The Component Object Model ...... 1140 18.1.1. Instantiating a COM Object ...... 1142 18.1.2. Object Lifetimes...... 1144 18.1.3. Acquiring Interface Pointers ...... 1145 18.1.4. COM Servers ...... 1146 18.1.5. Location Transparency...... 1148 18.1.6. Object Linking and Embedding ...... 1149 18.1.7. Active Documents...... 1152 18.1.8. ActiveX...... 1153 18.2. MFC and COM ...... 1154 18.2.1. Multiple Inheritance...... 1155 18.2.2. Nested Classes ...... 1157 18.2.3. MFC and Nested Classes ...... 1161 18.2.4. How MFC Implements IUnknown...... 1163 18.2.5. Interface Maps ...... 1165 18.2.6. MFC and Aggregation...... 1166 18.2.7. MFC and Class Factories ...... 1169 18.2.8. Putting It All in Perspective ...... 1171 Chapter 19. The Clipboard and OLE Drag-and-Drop ...... 1173 19.1. The Legacy Clipboard ...... 1174 19.1.1. Clipboard Formats ...... 1176 19.1.2. Private Clipboard Formats ...... 1181 19.1.3. Providing Data in Multiple Formats ...... 1182 19.1.4. Querying for Available Data Formats ...... 1183 7 19.1.5. Delayed Rendering ...... 1185 19.1.6. Building a Reusable Clipboard Class ...... 1189 19.2. The OLE Clipboard ...... 1189 19.2.1. OLE Clipboard Basics...... 1190 19.2.2. MFC, Global Memory, and the OLE Clipboard ...... 1196 19.2.3. Using Alternative Storage Media...... 1198 19.2.4. Treating the OLE Clipboard as a CFile ...... 1200 19.2.5. Multiple Formats and Multiple Storage Media...... 1202 19.2.6. Checking Data Availability...... 1203 19.2.7. Delayed Rendering with COleDataSource...... 1204 19.2.8. COleDataSource and COleDataObject in Review...... 1208 19.3. OLE Drag-and-Drop...... 1209 19.3.1. Anatomy of a Drop Source...... 1211 19.3.2. Anatomy of a Drop Target...... 1215 19.3.3. MFC Support for OLE Drag-and-Drop ...... 1218 19.3.4. Drop Target Scrolling ...... 1221 19.4. Putting It All Together: The Widget Application...... 1221 19.4.1. The AfxOleInit Function ...... 1245 Chapter 20. Automation ...... 1247 20.1. Automation Basics...... 1248 20.1.1. IDispatch: The Root of All Automation...... 1250 20.1.2. Automation Data Types...... 1252 20.1.3. Late Binding vs. Early Binding ...... 1259 20.1.4. Dual Interfaces ...... 1260 20.1.5. Type Libraries...... 1261 20.2. MFC Automation Servers...... 1263 20.2.1. MFC, IDispatch, and Dispatch Maps ...... 1264 20.2.2. Writing an Automation Server...... 1266 20.2.3. Automation Hierarchies...... 1274 20.2.4. A More Complex Automation Server...... 1276 20.3. MFC Automation Clients...... 1302 20.3.1. The PieClient Application ...... 1303 20.3.2. Connecting to a Running Automation Server...... 1316 Chapter 21. ActiveX Controls...... 1319 21.1. ActiveX Control Basics ...... 1321 21.2. Building ActiveX Controls...... 1333 21.3. Using ActiveX Controls in MFC Applications ...... 1375 21.4. Advanced Topics...... 1386
8 Programming Windows With MFC 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 to 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: