easy object programming for windows™ using visual c++™
TRANSCRIPT
Easy Object Programming for Windows™
Using Visual C++™
Richard O. Parker
P R E N T I C E HALL, Englewood Cliffs, New Jersey 07632
Table of Contents
Preface xix Acknowledgments xxi
Notation Used in This Book xxii
How to Use This Book xxii Installing the Included Source Code xxiii
Author's Final Note xxiv
Chapter 1
Learning to Use the Tools 1 Creating the Application Skeleton 1
Exercises 9
Chapter 2
Examining the Skeleton Code 11
Analyzing the CKeepitApp Code 12
The Message Loop 12
CKeepitApp Header File 14
CKeepitApp Source File 15 CKeepitApp Constructor, 16
Initlnstance Member Function, 17 CWinApp OnFileNew Member Function, 20
CAboutDlg Class Declaration, 20
CAboutDlg Source File, 21
CKeepitApp Command Handler Placeholder, 22
Analyzing the CMainFrame Code 23
CMainFrame Header File 23 CMainFrame Source File 25
Initial CMainFrame Code, 25
CMainFrame's Message Map, 26 Toolbar and Status Bar Definitions, 26
v
vi >
CMainFrame's Constructor and Destructor Code, 27
CMainFrame's OnCreate Handler, 27
CMainFrame Diagnostic Code, 29
CMainFrame Handler Placeholder, 29
Analyzing the CKeepitDoc Code 30
CKeepitDoc Header File 30
CKeepitDoc Source File 32
Initial CKeepitDoc Code, 32
The Message Map, 32
CKeepitDoc Constructor and Destructor, 32
OnNew^Document Member Function, 33
Serialize Member Function, 33
Diagnostic Member Functions, 34
Command Handler Placeholder, 35
Analyzing the CKeepitView Code 35
CKeepitView Header File 36
CKeepitView Source File 37
Initial CKeepitView Code, 37
CKeepitView Message Map, 38
CKeepitView Constructor and Destructor, 38
OnDraw Member Function, 39
Printing Support Member Functions, 39
Diagnostic Member Functions, 40
GetDocument Member Function, 41
CKeepitView Message Handler Placeholder, 41
Exercises 41
Chapter 3
Designing the Primary Form 43
Designing the New View 43
Changes to the CAcctList Class 52
Changes to the CListEdit Class 53
Changes to the CAccount Class 54
Changes to the CKeepitApp Class 56
Updating the Project Directory and Class Wizard Database 59
Rebuilding the ClassWizard Database 60
Creating a Super Browser 61
Exercises 64
vu
Chapter 4
Customizing the Primary Form's Code 67
The Account View Concept 67
Creating the Custom View 68
Creating the List of Entries 69
Creating the CListEntry Header File, 69
The Initial CListEntry Source Code, 71
Initializing a CListEntry Object, 72
Access Member Functions to Retrieve CListEntry Variables, 72
Access Member Functions to Store CListEntry Variables, 74
VerifyValue Member Function Code, 75
Handling the CListEntry Objects 76
Access Member Functions for the CObList Object, 78
OnNewDocument Member Function Code, 79
Initializing the View 80
OnlnitialUpdate Member Function Code, 80
Drawing the Form's Heading 83
OnDraw Member Function Code, 84
DrawLines Member Function Code, 85
Drawing the Contents of the List Entries 86
Measureltem Member Function Code, 86
Drawltem Member Function Code, 87
DrawEntry Member Function Code, 90
Compareltem Member Function Code, 95
Handling User Actions 96
Processing Keystrokes 97
OnGetDlgCode Member Function Code, 97
OnKeyDown Member Function Code, 97
OnChar Member Function Code, 98
HandleKeys Member Function Code, 99
Navigating From Field to Field 100
UpdateFields Member Function Code, 101
ValidDescription Member Function Code, 105
ValidCategory Member Function Code, 105
ValidEntry Member Function Code, 106
SetUpEditltem Member Function Code, 106
SelectNextEntry Member Function Code, 109
OnSelChgAcctList Message Handler Code, 109
Global Support Functions 112
Get2Nums Function Code, 112
Saving and Loading Account Data Files 113
Serializing the Document 113
Serialize Member Function Code, 114
Serializing the List Entries 115
viii >
CListEntry Serialize Member Function Code, 115
Summary of the Application's Features 116
Exercises 117
Chapter 5
Adding Accounts and Account Support 119
Additions and Changes to the Menu Bars 119
Creating the Category View 122
Creating the Edit Account Dialog 128
Resource Creation Recap 134
Creating Support for the Category View 135
Creating the New CCatList Class and Its Member Variables 135
Creating the CCatList Message Handler Functions 139
Creating Support for the Edit Account Dialog 142
Creating the New CEditAcct Class and Its Member Variables 143
Creating the CEditAcct Message Handler Functions 149
Adding Command Handlers to the CKeepitDoc Class 151
Examining the Newly Generated Code 153
Examining the CCatList Code 154
CatList.h Header File, 154
CatList.cpp Source File, 155
Examining the CEditAcct Code 156
EditAcct.h Header File, 157
EditAcct.cpp Source File, 158
Examining the Additions to the CKeepitDoc Class 159
KeepDoc.h Header File Additions, 159
KeepDoc.cpp Source File Additions, 160
Exercises 160
Chapter 6
Customizing the Account Features Code 163
Adding New Object Classes 164
Implementing the CCategory Class 165
CCategory Class Header File Code, 165
CCategory Class Source File Code, 166
Implementing the CAcctObj Class 168
CAcctObj Class Header File Code, 168
CAcctObj Class Source File Code, 170
Providing Support for Multiple Accounts 172
Enhancing the CKeepitApp Class 172
CKeepitApp Class Header File Code, 173
ix
CKeepitApp Constructor Source Code, 174 CKeepitApp Initlnstance Source Code, 175 Document Template String Resources, 176
Creating a New Child Frame Window 178 CMDIFormFrame Header File Code, 178 CMDIFormFrame Source File Code, 179
Updating the CKeepitDoc Class 180 CKeepitDoc Header File Code, 181 CKeepitDoc Constructor Function Code, 183 FillAcctList Member Function Code, 183 OnNewDocument Member Function Code, 184;
Serialize Member Function Code, 185 AddAcctMenu Member Function Code, 187 RemoveAcctMenu Member Function Code, 188 SetMenuNames Member Function Code, 188 DeleteContents Member Function Code, 189
Customizing and Supporting the Views 190 Customizing the CCatList View 190
CCatList Header File Code, 190 The CCatList Source File Beginning Code, 191 Adding the CCatList GetDocument Member Function, 192 OnlnitialUpdate Member Function Code, 193 FindCategory Member Function Code, 195 OnAdd Message Handler Code, 195 OnDelete Message Handler Code, 197 OnModify Message Handler Code, 199 OnSelectCat Message Handler Code, 200 OnKillfocusCategory Message Handler Code, 201
Customizing the CEditAcct Dialog 202 CEditAcct Header File Code, 203 CEditAcct Preprocessor Declarations, 204 OnlnitDialog Member Function Code, 204 SetAcctlnfo Member Function Code, 205 ClearSettings Member Function Code, 206 OnSelchangeAcctName Message Handler Code, 206 OnKillfocusAcctName Message Handler Code, 207 OnDeleteAcct Message Handler Code, 208 OnAddAcct Message Handler Code, 210 OnModifyAcct Message Handler Code, 211
Customizing the CAccount View 213 OnlnitialUpdate Member Function Code Changes, 213 ValidCategory Member Function Code, 214 OnDestroy Member Function Code, 215
Changes to the CListEntry Class Member Functions 215
x >-
InitEntiy Member Function Code, 216 MakeToday Member Function Code, 216
Customizing the CKeepitDoc Document Class Member Functions 216
OnViewCategories Message Handler Code, 217 OnAcctEdit Message Handler Code, 217 RemoveTransactions Member Function Code, 222 RemoveMDIFrame Member Function Code, 223 OnCmdMsg Member Function Code, 224 OnAccountName Member Function Code, 225 OnAccountNameUpdate Member Function Code, 227 CanCloseFrame Member Function Code, 228
Running the Keepit Application 228
Exercises 230
Chapter 7
Adding Support for Reports 233
Creating the Report Menu and Its Items 233
Creating the Report Settings Dialog 235
Creating the CSettings Class and Member Variables 241
Defining Message Handlers for the CSettings Class 246
Creating the List of Accounts Dialog 248
Creating the CSelectedAccts Class and Member Variables 251
Defining Message Handlers for the CSelectedAccts Class 254
Creating the CReportView Class and Its Message Handlers 256
Adding a Handler to the CKeepitDoc Class 257
Examining the Newly Generated Code 259
Examining the Additions to the CKeepitDoc Class 259
KeepDoc.h Header File Additions, 259
KeepDoc.cpp Source File Additions, 260
Examining the CSettings Code 260
Settings.h Header File Code, 260
Settings.cpp Source File Code, 261
Examining the CSelectedAccts Code 263
Selected.h Header File Code, 263
Selected.cpp Source File Code, 264
Examining the CReportView Code 265
Reportvw.h Header File Code, 265
Reportvw.cpp Source File Code, 266
Exercises 268
XI
Chapter 8
Customizing the Reports Code 269 Adding a New Document Template 270 Document Support for the CReportView Creation 271
CKeepitDoc Header File Additions 271 New Definitions, 271 New Member Variables, 272 • New DeleteDlgAccts Support Member Function, 272 Message Map Function Declarations, 272
CKeepitDoc Source File Additions 273 New Preprocessor Directives, 273 Newly Generated Message Map, 273 OnCashFlow Message Handler Code, 273 DeleteDlgAccts Member Function Code, 277
Customizing the Settings Dialog Code 277 CSettings Header File Changes 277
Newly Added Preprocessor Definitions, 278 OnlnitDialog Override Declaration, 278 Newly Added Member Variables, 278
CSettings Source Code Additions 278 Newly Added Preprocessor Statements, 279 DoDataExchange Member Function Code, 279 Newly Added Validation Types, 279 OnlnitDialog Member Function Code, 282 OnPerListSelect Message Handler Code, 283 OnAcctsClicked Message Handler Code, 285
Data Exchange and Validation 287 Dattim Header File Declarations 287 Dattim Source File Code 287
Source File Definitions, 287 DDX_DateCheck Function Code, 288 GetDate Helper Function Code, 289 MakeDateObj Helper Function, 291
Customizing the CSelectedAccts Code 291 CSelectedAccts Header File Additions 291 CSelectedAccts Source File Additions 292
OnlnitDialog Member Function Code, 292 OnSelectAccount Message Handler Code, 293 OnAll Message Handler Code, 294 OnNone Message Handler Code, 294
Customizing the CReportView Code 295 CReportView Header File Additions 296 CReportView Source File Additions 298 Report Initialization Code 298
xii ^
OnlnitialUpdate Member Function Code, 299
SortByDate Member Function Code, 301
ComputeRptMetrics Member Function Code, 303
CashFlowViewSize Member Function Code, 306
Report-Drawing Code 310
OnPrepareDC Member Function Code, 310
OnDraw Message Handler Code, 311
DrawRptLine Member Function Code, 313
RectLPtoPositions Member Function Code, 316
OnHScroll Message Handler Code, 317
OnVScroll Message Handler Code, 318
Printing and Print Preview Code 318
OnFilePrint Message Handler Code, 319
OnFilePrintPreview Message Handler Code, 319
OnPreparePrinting Member Function Code, 320
OnBeginPrinting Member Function Code, 320
OnPrint Member Function Code, 322
PrintPageHeader Member Function Code, 325
Final Cleanup When the Report Window Is Closed 327
OnDestroy Message Handler Code, 327
Exercises 327
Chapter 9
Adding Custom Report Features 331
Adding a New Report Command 331
Enhancing the Settings Dialog 334
Adding the Options Button 335
Adding the Message Handler and Member Variables 336
Prototyping New Settings Dialog Options 338
Prototyping the Categories Options 339
Prototyping the Description and Info Options 342
Examining the Newly Generated Code 344
Examining the Additions to the CKeepitDoc Class 345
KeepDoc.h Header File Additions, 345
KeepDoc.cpp Source File Additions, 345
Examining the Additions to the CSettings Class 345
Settings.h Header File Additions, 346
Settings.cpp Source File Additions, 346
Exercises 347
Xlll
Chapter 10
Additional Customizing of the Reports Code 349 Support for the Settings Dialog Options 349
Newly Defined Control Identifiers 350 CSettings Class Header File Declarations 351
Settings.h Initial Section, 352 Dynamic (DDX) and General Variable Declarations, 352 Message Handler Function Declarations, 353 Standard Member Functions and Member Variables, 353 Member Functions and Variables for the Cash Flow Report, 354
CSettings Class Source File Definitions 356 Settings.cpp Initial Section, 356 Dynamic Control Position Definitions, 357 CSettings Constructor Member Function, 357 Dlu2Pix Member Function Code, 358 RectAdd and RectSub Member Function Code, 358 OnlnitDialog Member Function Code, 359 DoDataExchange Member Function Code, 361 Message Map Code, 362 OnPerListSelect Message Handler Code, 363 OnAcctsClicked Message Handler Code, 364 DeleteStdControls Member Function Code, 365 DeleteOptControls Member Function Code, 366 FindCategory Member Function Code, 367 AddCategories Member Function Code, 367 SelectCategories Member Function Code, 368 MakeStandardControls Member Function Code, 369 MakeCashFlowControls Member Function Code, 370 OnOptions Message Handler, 372
Settings Dialog OnDestroy Handler 374 Settings Dialog Dynamic Message Handlers 374
OnTypeSelect Message Handler Code, 375 OnCatSelect Message Handler Code, 375 OnCatRadio Message Handler Code, 376 MakeTextSettings Helper Member Function Code, 377 OnStringEntry Message Handler Code, 379 OnContentsRadio Message Handler Code, 379
KeepType.h Header File Contents 380 Additions to the CKeepitDoc Class 381
KeepDoc.h Header File Contents 382 KeepDoc.cpp Source File Additions 384
CKeepitDoc Message Map Code, 384 DeleteDlgAccts Member Function Code, 384 ReportSetup Member Function Code, 385
xiv •
OnCashFlow Message Handler Code, 388 OnNetWorth Message Handler Code, 388
CReportView Class Additions 389 ReportView.h Header File Contents 389 Report Vi ew.cpp Source File Contents 392
CReportView Source File Beginning, 393 OnlnitialUpdate Member Function Code, 394 OnPrepareDC Member Function Code, 396 ComputeRptMetrics Member Function Code, 397 OnDraw Member Function Code, 398 DrawRptLine Member Function Code, 400 CashFlowViewSize Member Function Code, 404 NetWorthViewSize Member Function Code, 406 MeetsRptCriteria Member Function Code, 408 MatchStrings Member Function Code, 410 ComputeBalance Member Function Code, 413 MakeValueString Member Function Code, 414 MakeRptListEntry Member Function Code, 415 MakeAcctEntry Member Function Code, 415 SortByDate Member Function Code, 416 RectLPtoPositions Member Function Code, 417 OnPreparePrinting Member Function Code, 418 OnBeginPrinting Member Function Code, 418 OnPrint Member Function Code, 419 PrintPageHeader Member Function Code, 419 OnFilePrint Message Handler Code, 420 OnFilePrintPreview Message Handler Code, 421 OnDestroy Message Handler Code, 421 OnHScroll Message Handler Code, 422 OnVScroll Message Handler Code, 422
CReportLine Class Additions 423 RepLine.h Header File Contents 424 RepLine.cpp Source File Contents 425
Viewing the Results of Our Work 426 Exercises 426
Chapter 11
Adding Support for Charts 429 Adding the Net Worth Chart Command 429 Prototyping New Settings Dialog Options 431
Prototyping the Graph Type Options 432 Prototyping the Interval Options 435 Prototyping the Colors Options 437
XV
Cleaning Up Your Resource Files 439 Cleaning Up the Keepit.rc Resource File, 440 Cleaning Up the Resource.h Header File, 443
Examining the Newly Generated Code 444 Examining the Additions to the CKeepitDoc Class 444
KeepDoc.h Header File Additions, 444 KeepDoc.cpp Source File Additions, 444
Exercises 445
Chapter 12
Customizing the Chart Code 447 Support for the New Settings Dialog Options 447
Newly Defined Control Identifiers 447 CSettings Class Header File Declarations 449
Settings.h Initial Section, 449 Dynamic (DDX) and General Variable Declarations, 450 Message Handler Function Declarations, 450 Standard Member Functions and Member Variables, 451 Member Functions and Variables for the Cash Flow Report, 452 Member Functions and Variables for the Net Worth Chart, 453
CSettings Class Source File Definitions 455 Settings.cpp Initial Section, 455 Dynamic Control Position Definitions, 455 CSettings Constructor Member Function, 457 Dlu2Pix Member Function Code, 457 RectAdd Member Function Code, 458 RectSub Member Function Code, 458 MakeColorButton Member Function Code, 458 GetSamplelndex Member Function Code, 459 RedrawButton Member Function Code, 459 OnlnitDialog Member Function Code, 460 DoDataExchange Member Function Code, 463 Message Map Code, 463 OnPerListSelect Message Handler Code, 465 OnAcctsClicked Message Handler Code, 466 DeleteStdControls Member Function Code, 467 DeleteOptControls Member Function Code, 468 FindCategory Member Function Code, 470 AddCategories Member Function Code, 470 SelectCategories Member Function Code, 470 MakeStandardControls Member Function Code, 471 MakeCashFlowControls Member Function Code, 472 MakeChartControls Member Function Code, 474
OnOptions Message Handler Code, 477 OnDestroy Message Handler Code, 478 OnTypeSelect Message Handler Code, 479 OnCatSelect Message Handler Code, 479 OnCatRadio Message Handler Code, 480 MakeTextSettings Helper Member Function Code, 481 OnStringEntry Message Handler Code, 482 OnContentsRadio Message Handler Code, 482 OnGraphTypeRadio Message Handler Code, 483 OnTitleEntry Message Handler Code, 483 OnlntervalRadio Message Handler Code, 484 OnUseColorCheck Message Handler Code, 484 OnSampleClick Message Handler Code, 484 OnColorClick Message Handler Code, 485
Newly Added CCButton Class Code 486 CCButton Class Header File Declarations 486
CCButton.h Contents, 487 CCButton Source File Definitions 487
First Section of the CCButton.cpp Source File, 487 Constructor and Destructor Code, 488 Drawltem Member Function Code, 489 RGBColor Global Function Code, 490
Additions to the CKeepitDoc Class 490 KeepitDoc.h Header File Additions 491 KeepDoc.cpp Source File Additions 493
Message Map Code, 493 OnChrtNetWorth Message Handler Code, 493 ReportSetup Helper Member Function Code, 493
Newly Added CReportView Code 496 CReportView Header File Additions 498
Reportvw.h First Section, 498 Public Attribute Declarations, 498 Member Function Declarations, 499 Message Map Declarations, 500 Protected Member Variable Declarations, 500 In-line GetDocument Member Function for Nondebug Version, 501
Reportvw.cpp Source File Additions 501 Initial Definitions, 502 Constructor and Destructor Member Functions, 502 Message Map Entries, 503 OnlnitialUpdate Member Function Code, 503 OnPrepareDC Member Function Code, 506 ComputeRptMetrics Member Function Code, 506
OnDraw Member Function Code, 507 DrawRptLine Member Function Code, 509 DrawChart Member Function Code, 512 GetDocument Member Function Code, 516 CashFlowViewSize Member Function Code, 517 NetWorthViewSize Member Function Code, 519 NetWorthChartSize Member Function Code, 520 MeetsRptCriteria Member Function Code, 523 MatchStrings Member Function Code, 524 ComputeBalance Member Function Code, 527 MakeValueString Member Function Code, 528 MakeRptListEntry Member Function Code, 528 MakeAcctEntry Member Function Code, 529 NextMonth Member Function Code, 530 NextQuarter Member Function Code, 530 NextYear Member Function Code, 530 IntervalNetWorth Member Function Code, 531 MakeChartEntry Member Function Code, 531 SortByDate Member Function Code, 532 RectLPtoPositions Member Function Code, 532 OnPreparePrinting Member Function Code, 533 OnBeginPrinting Member Function Code, 534 OnPrint Member Function Code, 534 PrintPageHeader Member Function Code, 535 OnFilePrint Message Handler Code, 536 OnFilePrintPreview Message Handler Code, 536 OnDestroy Message Handler Code, 536 OnHScroll Message Handler Code, 537 OnVScroll Message Handler Code, 537
Additions to the CReportLine Class 538 RepLine.h Header File Additions 538 RepLine.cpp Source File Additions 539
Application Summary 540 Exercises 540
Index 543