m^ Developing Professional Applications in Windows 95 and NT sing MFC Marshall Brain Lance Lovette To join a Prentice Hall PTR internet mailing list, point to http://www.prenhall.com/register Prentice Hall PTR Upper Saddle River, New Jersey 07458 http://www.prenhall.com
Preface Version Free xiii Audience xiv Organization xiv The CD-ROM and the On-line Index xv Contacting the Authors: Questions, Comments, and Version Changes xv Acknowledgments xvi xiii Getting Your Bearings 1 What is Visual C++? 2 Available Documentation 3 7. Road Map 4 Common Questions 5 Part 1 Visual C++and MFC Basics, 11 1 Introduction 13 1.1 What is the Microsoft Foundation Class Library? 13 1.2 Windows Vocabulary 14 1.3 Event-driven Sofiware and Vocabulary 16 1.4 An Example 19 1.5 Conclusion 20 2 Understanding an MFC Program 21 2.1 An Introduction to MFC 21 2.2 Designing a Program 23 2.3 Understanding the Code for "Hello World" 23 2.4 Completing the Program 30 2.5 MFC Application Structure 32 2.6 Conclusion 32 3 Customizing Controls 33 3.1 The Basics 33 3.2 CStatic Styles 36 3.3 CStatic Text Appearance 37
3.4 Rectangular Display Modes for CStatic 44 3.5 Fonts 47 3.6 Conclusion 48 4 Handling Events 49 4.1 Understanding Message Maps 49 4.2 The CButton Class 50 4.3 Creating a Message Map 52 4.4 Sizing Messages 54 4.5 Window Messages 57 4.6 Scroll Bar Controls 60 4.7 Understanding Message Maps 64 4.8 Conclusion 64 5 Simple Applications 67 5.1 Designing an Application 67 5.2 Implementing the Fahrenheit to Celsius Converter 69 5.3 The CEdit Control 74 5.4 An Interest Calculator 77 5.5 Conclusion 81 Part 2 Visual C+ + and MFC Details 83 6 Resources, Dialogs, and Menus 85 6.1 Resources and Resource Files 85 6.2 The Icon Resource 88 6.3 Creating a Resource File 90 6.4 Menus 92 6.5 Responding to Menus 96 6.6 Dialog Resources 100 6.7 String Resources 106 6.8 Conclusion 109 7 Canned Dialogs 111 7.1 The Message Box Dialog 111 7.2 The File Open/Save Dialog 115 7.3 The Font Dialog 120 7.4 The Color Dialog 123 7.5 The Print Dialog 124 7.6 The Find/Replace Dialog 127
77 Conclusion 132 8 Edit Controls and Editors 133 8.1 Using the CEdit Control in Single-Line Mode 133 8.2 Using the CEdit Control in Multi-Line Mode 136 8.3 Designing a Simple Text Editor 139 8.4 Creating the Editor Application 140 8.5 Stubbing in the Menu Handlers 144 8.6 Implementing the Editor 150 8.7 Conclusion 161 9 Lists 163 9.1 Creating a List Box 163 9.2 Alternate Display Formats 167 9.3 Getting User Selections 170 9.4 Manipulating Items in a List 174 9.5 Combo Boxes 175 9.6 Conclusion 177 10 The CWinApp class 179 10.1 Member Variables 179 10.2 Icons and Cursors 181 10.3 Handling Idle Time 185 10.4 Application Functionality 186 10.5 Initialization Features 190 10.6 Miscellaneous Features 192 10.7 Conclusion 192 11 Drawing 195 11.1 Introduction to the GDI Library 195 11.2 GDI Basics 196 11.3 Device Contexts 198 11.4 Simple Drawing 200 11.5 Using the Mouse with Your Drawings 222 11.6 Advanced Drawing Concepts 249 11.7 Conclusion 261 12 Utility Classes 263 12.1 Utility Classes 264 12.2 Simple Array Classes 278 12.3 The CObject class and CObject Arrays 282 12.4 List Classes 291
12.5 Mapping Classes 295 12.6 Conclusion 298 13 Debugging and Robustness 299 13.1 Setting Up 299 13.2 Assertions 300 13.3 Tracing 305 13.4 Dumping 307 13.5 Memory State 309 13.6 Exceptions 313 13.7 Other Debugging Features 321 13.8 Conclusion 321 Part 3 Using; the Visual C+ + Wizards and Tools to Create Applications 323 14 Understanding the AppWizard and ClassWizard 325 14.1 The Goal of the App Wizard 325 14.2 Creating a Simple Framework with the AppWizard 327 14.3 The AppWizard's Document-Centric Approach 327 14.4 Understanding the AppWizard's Files 329 14.5 Understanding the ClassWizard 333 14.6 Conclusion 335 15 Creating a Drawing Program 337 15.1 The Goal of the Application 337 15.2 Creating a Drawing Program 338 15.3 Understanding the Drawing Program 347 15.4 Creating an MDI Application 352 15.5 Scrolling 356 15.6 Splitter Windows 362 15.7 Adding New Menu Options and Dialogs. 371 15.8 Printing 380 15.9 Conclusion 388 16 Creating an Editor with CEditView 391 16.1 Creating an MDI Text Editor 391 16.2 Understanding the Editor 392 16.3 Combining Two Documents and Views in a Single
Application 394 16.4 Fixing a Subtle Problem 397 16.5 Handling Multiple Views on One Document 398 16.6 Conclusion 399 17 Creating a Fahrenheit-to-Celsius Converter 401 17.1 Creating the Converter 401 17.2 Understanding the Program 404 17.3 Using DDX 405 17.4 Using the Document Class 406 17.5 Using Form Views 410 17.6 Conclusion 411 1.8 Creating an Address List Application 413 18.1 Creating the Application 413 18.2 Understanding the Address List Program 424 18.3 Understanding DDX and DDV 425 18.4 Improving the Application 427 18.5 Printing 445 18.6 Conclusion 450 19 Context-Sensitive Help 453 19.1 Understanding the App Wizards Help Framework 453 19.2 Understanding and Modifying the Help Files 454 19.3 Context-Sensitive Help 460 19.4 Aliases 463 19.5 Conclusion 463 20 Common Controls 465 20.1 A Simple Example Using the Spin Button, List, and Tree Controls 465 20.2 CSpinButtonCtrl 466 20.3 CListCtrl 466 20.4 CTreeCtrl 469 20.5 Property Sheets 470 20.6 A Property Sheet Example 470 20.7 The CPropertySheet Class 471 20.8 Conclusion 473
21 Creating Explorers 475 21.1 Creating the basic framework 476 21.2 Conclusion 480 Part 4 Advanced Features 481 22 Dialog Data Exchange and Validation 483 22.1 Understanding DDX 484 22.2 Exchange Routines 485 22.3 Transfer Direction 486 22.4 Understanding DDV 486 22.5 An Example 487 22.6 Custom Routines 493 22.7 Conclusion 494 23 Understanding MFC 495 23.1 What Are Window Handles? 495 23.2 The Life of Windows and Objects 497 23.3 Initializing Dialogs 498 23.4 From HWND to CWnd 499 23.5 Permanent and Temporary Associations 501 23.6 Handles to Other Objects 501 23.7 How Messages Work 501 23.8 Subclassing 503 23.9 Conclusion 504 24 Enhancing The Edit Control 505 24.1 An Example 505 24.2 Understanding the Process 506 24.3 Conclusion 507 25 Self-Managing Controls 509 25.1 Owner-Drawing vs. Self-Drawing 510 25.2 Owner-Drawn Messages 510 25.3 The Self Drawing Framework 510 25.4 Behind the Scenes 511 25.5 A General Solution 511
25.6 A Self-Drawing Combo Box 513 25.7 Drawing Transparent Bitmaps 517 25.8 Subclassing the Combo Box 520 25.9 Conclusion 521 26 Another Look 523 A Self-Drawing List Box 26.1 Introduction to Font Enumeration 523 26.2 Enumerating Font Families 524 26.3 Enumerating Font Styles 527 26.4 An Example 528 26.5 Conclusion 532 / Creating A Splash Screen 533 27.1 An Example 533 27.2 Conclusion 537 28 Expanding Dialogs 539 28.1 The CExpandingDialog Class 539 28.2 An Example 545 28.3 Conclusion 546 29 Drawing and Controls 547 29.1 Drawing in CStatic Controls 547 29.2 Drawing in Dialogs 550 29.3 Dialog Controls and the Background 552 29.4 Conclusion 552 30 Dialog Bars 555 30.1 An Example 556 30.2 Data Exchange 558 30.3 Conclusion 559 31 Dialog and View Idle Command Updating 561 31.1 How Idle Updates Work 561 31.2 Idle Updating in Views 562 31.3 An Example 563 31.4 Idle Updating in Dialogs 563 31.5 An Example 565 31.6 Conclusion 566
32 Odds and Ends 567 32.1 Accepting Files from the File Manager 567 32.2 Making an Application the Topmost Window 568 32.3 Starting an Application Minimized 569 32.4 Modeless Dialog Boxes 569 32.5 Mini-Frame Windows 571 32.6 Context Popup Menus 574 32.7 Modifying the System Menu 576 32.8 Conclusion 576 Part 5 Advanced MFC Classes 577 33 Database Access 579 33.1 Understanding Relational Databases 579 33.2 Understanding SQL 582 33.3 Understanding ODBC 585 33.4 Microsoft Query 586 33.5 The CRecordSet Class 591 33.6 Simple CRecordSet Operations 593 33.7 Using the CRecordView Class 602 33.8 Adding and Deleting Records 605 33.9 Conclusion 607 34 OLE 609 34.1 Understanding OLE 609 34.2 An Example 614 34.3 OLE as a Vision of the Future 617 34.4 Standard OLE Features 618 34.5 An Introduction to OLE Containers 621 34.6 An Lntroduction to OLE Servers 629 34.7 An Introduction to OLE Automation 636 34.8 An Introduction to OLE Controls 640 34.9 Conclusion 650 35 MFC Threads 651 35.1 Understanding the Possibilities 651 35.2 Understanding Threads 652 35.3 MFC Worker Threads 655 35.4 Thread Termination 657
35.5 Passing Parameters to Threads 660 35.6 Suspending and Resuming Threads 661 35.7 Thread Priorities 662 35.8 Subclassing CWin Thread 667 35.9 User Interface Threads 675 35.10 Conclusion 677 A В Understanding C++: An Accelerated Introduction 679 Using the Visual C++ Compiler and Tools 739 B. 1 Compiling and Executing a Console Program with Visual C++ 739 B.2 Debugging 744 B.3 Compiling MFC Programs 747 B. 4 The Browser 752 B.5 Resources and resource files 756 B.6 AppWizard Files 761 B.7 Using the ClassWizard 769 B.8 OLE Controls 775 B.9 Conclusion 781 С Contacting the Authors 783 D Using OpenGL with MFC 785 D.l Writing an OpenGL Program 785 D.2 Simple 2-D Graphics 791 D.3 Transformations and the Matrix Stack 793 D.4 Mouse Action 796 D.5 Double Buffering 797 D.6 A Three Dimensional Cube 798 D.7 Z-Buffering 801 D. 8 Conclusion 802 Index 803