`
`•
`I
`
`• SI
`•
`I
`
`• SI
`Is
`
`st
`
`g
`
`= = = :::-:::
`
`C.r.>
`
`HENRY HOLT & COMPANY, INC.
`NEW YORK
`
`NFLE 1008 - Page 2
`
`
`
`M&T Books
`A Division of MIS:Press, Inc.
`A Subsidiary of Henry Holt and Company, Inc.
`115 West 18th Street
`New York, New York 10011
`http:/ /www.mispress.com
`Copyright© 1997 by M&T Books
`Printed in the United States of America
`All rights reserved. No part of this book may be reproduced or transmitted in any form or by
`any means, electronic or mechanical, including photocopying, recording, or by any information
`storage and retrieval system, without prior written permission from the Publisher. Contact the
`Publisher for information on foreign rights.
`Limits of Liability and Disclaimer of Warranty
`The Author and Publisher of this book have used their best efforts in preparing the book and
`the programs contained in it. These efforts include the development, research, and testing of the
`theories and programs to determine their effectiveness.
`The Author and Publisher make no warranty of any kind, expressed or implied, with regard to
`these programs or the documentation contained in this book. The Author and Publisher shall
`not be liable in any event for incidental or consequential damages in connection with, or arising
`out of, the furnishing, performance, or use of these programs.
`All products, names and services are trademarks or registered trademarks of their respective
`companies.
`First Edition-1997
`ISBN 1-55851-503-8
`
`MIS:Press and M&T Books are available at special discounts for bulk purchases for sales pro(cid:173)
`motions, premiums, and fundraising. Special editions or book excerpts can also be created to
`specification.
`For details contact:
`
`Special Sales Director
`MIS:Press and M&T Books
`Subsidiaries of Henry Holt and Company, Inc.
`115West18th Street
`New York, New York 10011
`
`10 9 8 7 6 5 4 3 2 1
`
`Associate Publisher: Paul Farrell
`Production Editor: Anthony Washington
`Executive Editor: Cary Sullivan
`Editor: Andrew Neusner
`Technical Editor: Mark Bramer
`Copy Edit Manager: Shari Chappell
`Copy Editor: Betsy Hardinger
`
`NFLE 1008 - Page 3
`
`
`
`Chapter 1: Component-Based Development, the Web, and ActiveX •••••••••• 1
`
`Chapter 2: Designing Reusable Components with C++ •••••••••••••••••• 17
`Chapter 3: Visual C++ and the MFC Libraries •••••••••••••••••••••••• • 77
`
`Chapter 4: Microsoft's Component Obied Model ••••••••••••••••••••• 113
`Chapter 5: COM, OLE, AdiveX, and the MFC Libraries •••••••••••••••••• 187
`
`Chapter 6: Automation ••.••.•••••••••..••••.•••••••••••.•••••• . 221
`
`Chapter 7: ActiveX Controls •••••••••••••••••••••••••••••••••••••• 291
`Chapter 8: A Simple Control ••••.••.••.••••.••••.•.•.••.••••••.• • 335
`
`Chapter 9: Graphical Controls ••••••••••••.••••••••••••••••••••••• 393
`
`Chapter 10: Subclassing Windows Controls •••••••••••••••••••••••••• 453
`Chapter 11 : Nonvisual Controls ••••••••••••••••••••••••••••••••••• 511
`
`Chapter 12: Internet-Aware Controls ••••••••••••••••••••••••••••••• 549
`
`Chapter 13: AdiveX Control Frequently Asked Questions ••••••••••••••• 583
`
`Appendix A: CD-ROM Instructions •••••••••••••••••••••••••••••••••• 615
`Appendix B: Bibliography ••••••••••••••••••••••••••••••••••••••• 619
`
`NFLE 1008 - Page 4
`
`
`
`To Nicole, Jessica, and Eric. Your love and support empower me to achieve things I never dreamed possi(cid:173)
`ble.
`
`NFLE 1008 - Page 5
`
`
`
`
`
`vi + Contents
`
`Chapter 2: Designing Reusable Components with C++ •••••••••••••••••• 17
`C++, the Language ........................................................ 18
`Classes (Encapsulation) ..................................................... 18
`Constructors
`. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
`Th;i new and delete Operators ................................................ 23
`Destructors
`. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
`Inheritance .............................................................. 26
`Public, Protected, and Private ................................................. 31
`Function Overriding
`....................................................... 34
`Function Overloading ...................................................... 36
`Virtual Functions .......................................................... 38
`Pure Virtual Functions and Abstract Classes ...................................... 40
`Understanding Vtables ..................................................... 41
`Multiple Inheritance ........................................................ 44
`Class Composition ......................................................... 45
`The const Keyword ........................................................ 47
`References
`. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
`The this Keyword .......................................................... 49
`Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
`Overloading Operators ..................................................... 51
`Static Class Members ....................................................... 51
`The Problem ............................................................. 54
`Solving the Problem with a Reusable Class ....................................... 54
`Interface versus Implementation ............................................... 56
`The Expression Class Implementation ........................................... 57
`Infix and Postfix Expressions .................................................. 59
`A Tokenizer Class ......................................................... 67
`A Stack Class ............................................................ 71
`Summary ............................................................... 76
`
`Chapter 3: Visual C++ and the MFC Libraries •••••••••••••••••••••••• • 77
`Win 16 versus Win32 Development ........................................... .77
`Visual C++ .............................................................. 78
`Application Frameworks .................................................. 78
`
`NFLE 1008 - Page 7
`
`
`
`Contents + vii
`
`The Microsoft Foundation Class Libraries ........................................ 79
`An MFC Application that Evaluates Expressions ................................. 80
`Using AppWizard ......................................................... 80
`MFC Application Class Hierarchy .............................................. 85
`The Document/View Architecture ............................................ 86
`AppWizard-Generated Files ................................................. 87
`(Document ............................................................ 87
`CView ............................................................... 89
`CFrameWnd ........................................................... 90
`CDoc Template ......................................................... 91
`CWinApp ............................................................. 91
`Editing and Adding Resources ................................................ 97
`Class Wizard ............................................................. 99
`Message Maps ..................................................... '. .. 1 00
`Message Types ........................................................ 1 02
`Window Messages and Control Notifications .................................. 102
`Command Messages .................................................... 1 04
`Adding the Expression Class ................................................ 105
`MFC Debugging Techniques ................................................ 109
`Summary .............................................................. 112
`
`Chapter 4: Microsoft's Component Object Model ••••••••••••••••••••• 113
`A Binary Standard ....................................................... 114
`Component Interfaces ..................................................... 114
`Standard COM Interfaces .................................................. 117
`Component Lifetimes ...................................................... 121
`Multiple Interfaces ........................................................ 1 25
`Multiple Inheritance ..................................................... 126
`Interface Implementations ................................................ 127
`C++ Class Nesting ..................................................... 133
`GUIDs ................................................................ 137
`The Windows Registry ..................................................... 139
`Class Factories .......................................................... 143
`Where Do Components Live? ................................................ 146
`
`NFLE 1008 - Page 8
`
`
`
`viii + Contents
`
`Marshaling ............................................................. 147
`Distributed COM ......................................................... 148
`Custom COM Interfaces .................................................... 149
`Describing a Component ................................................... 149
`Component Categories .................................................. 149
`Type Information ....................................................... 150
`COM Containment and Aggregation .......................................... 150
`The COM API ........................................................... 151
`CoBuildVersion ( 16-bit Only) .............................................. 152
`Colnitialize ........................................................... 152
`CoUninitialize ......................................................... 152
`CoRegisterClassObject .................................................. 152
`CoGetClassObject ..................................................... 153
`CoCreatelnstance and CoCreatelnstanceEx ................................... 154
`DllCanUnloadNow ..................................................... 155
`DllGetClassObject ...................................................... 1 55
`Client/Server Flow ....................................................... 156
`COM C++ Macros, BSTRs, and So On ......................................... 159
`BSTR ............................................................... 161
`HRESULT and SCODE ................................................... 161
`An Example ............................................................ 163
`The Expression Class as a COM Component ..................................... 163
`Register the Component .................................................... 175
`A Quick Test of the In-Process Server .......................................... 17 6
`A COM Client Application .................................................. 177
`Debugging the Client Application ............................................. 185
`Summary .............................................................. 185
`
`Chapter 5: COM, OLE, Active.X, and the MFC Libraries •••••••••••••••••• 187
`What Is COM? .......................................................... 187
`What Is OLE? ........................................................... 188
`What Is ActiveX? ......................................................... 188
`MFC and ActiveX ........................................................ 189
`Interfaces and Grouped Functionality .......................................... 190
`
`NFLE 1008 - Page 9
`
`
`
`Contents + ix
`
`Converting the Expression Examples to Use MFC ................................. 191
`Converting the Chapter 4 Client Application ..................................... 192
`Initializing the ActiveX Environment ........................................... 196
`Converting the Chapter 4 Server Application .................................... 197
`CCmdT arget ............................................................ 200
`Class Factories .......................................................... 21 0
`COleObjectFactory ....................................................... 211
`COleObjectFactory::Register .............................................. 215
`COleObjectFactory::RegisterAll ............................................ 215
`COleObjectFactory::Revoke and RevokeAll
`................................... 216
`COleObjectFactory::UpdateRegistry and UpdateRegistryAll ........................ 216
`MFC COM Helper Functions ................................................ 218
`A Recap of the Example Applications .......................................... 219
`Summary ............................................................... 220
`
`Chapter 6: Automation .•••.••••.•.•.••••.•.•..•.••••.•••.••.•• • 221
`What Is ActiveX Automation? ................................................ 221
`Automation Controllers .................................................... 222
`Visual versus Nonvisual Components .......................................... 223
`The I Dispatch Interface ..................................................... 223
`Invoke .............................................................. 224
`GetlDsOfNames ....................................................... 225
`GetT ypelnfo .......................................................... 226
`GetT ypelnfoCount ...................................................... 226
`Automation Properties and Methods ........................................... 226
`Automation Data Types .................................................... 228
`The VARIANT Data Type ................................................. 228
`The Safe Array ........................................................ 230
`A Native !Dispatch-Based Component ....................................... 230
`The Expression Class as an ActiveX Component .................................. 231
`Building the Visual C++ Project ............................................ 231
`Updating SERVER.Hand SERVER.CPP ....................................... 232
`Modifying EXPSVR.H and EXPSVR.CPP ...................................... 233
`Using Visual Basic as an Automation Controller .................................. 243
`
`NFLE 1008 - Page 10
`
`
`
`x. +Contents
`
`A Non-MFC Automation Controller ........................................... 245
`MFC and I Dispatch ....................................................... 254
`An Example MFC-Based Automation Server ..................................... 255
`Wrapping the Expression Class .............................................. 256
`MFC' s Dispatch Macros .................................................... 262
`Local Server Differences .................................................... 266
`Type Information ......................................................... 268
`Dual Interfaces .......................................................... 271
`Late Binding .......................................................... 272
`ID Binding ........................................................... 272
`Early Binding ......................................................... 273
`A Visual C++ Automation Controller .......................................... 273
`COleDispatchDriver .................................................... 276
`Automating an MFC Application ............................................. 279
`Standard Application Properties .............................................. 287
`Driving the Autosvr Example ................................................ 288
`Summary .............................................................. 289
`
`Chapter 7: Ac:tiveX Controls •••••••••••••••••••••••••••••••••••••• 291
`OLE's Compound Document Architecture ....................................... 291
`Compound Document Containers and Embedded Servers ........................... 292
`ActiveX Controls ......................................................... 293
`Types of ActiveX Controls ................................................... 294
`ActiveX Controls as Software Components ...................................... 294
`Some Terminology ....................................................... 295
`Ul-Active Object ....................................................... 295
`Active Object ......................................................... 295
`Embeddable Object .................................................... 296
`Passive Object ........................................................ 296
`Visual Editing and In-Place Activation
`....................................... 296
`Outside-In Object ...................................................... 296
`Inside-Out Object ...................................................... 296
`ActiveX Control Containers ................................................. 297
`Container Modalities ...................................................... 297
`
`NFLE 1008 - Page 11
`
`
`
`Contents -+ xi
`
`Control and Container Interfaces ............................................. 298
`ActiveX Controls and Containers: A History ..................................... 299
`The OLE Controls 94 Specification .......................................... 300
`OLE Controls 96 Specification ............................................. 30 l
`Control and Container Guidelines Version 2.0 ................................. 304
`ActiveX Controls for the Internet .............................................. 305
`ActiveX Control Functional Categories ......................................... 305
`Standard COM Object Interfaces ............................................. 306
`Compound Document Interfaces .............................................. 306
`Automation Support ...................................................... 307
`Properties ............................................................ 307
`Standard and Stock Properties ............................................. 307
`Ambient Properties ..................................................... 308
`Control Methods ......................................................... 31 0
`Properly Pages
`. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
`ISpecifyProperlyPages ................................................... 312
`IProperlyPageSite ...................................................... 313
`IProperlyPage2 ........................................................ 313
`Properly Persistence ....................................................... 314
`Connectable Objects and Control Events ........................................ 315
`Standard Events ....................................................... 317
`Custom Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 8
`Keystroke Handling ....................................................... 318
`Control Containment ...................................................... 319
`MFC and ActiveX Controls .................................................. 319
`Visual C++ and ActiveX Control Support ....................................... 320
`Visual C++ Version 2.0 (MFC 3.0) .......................................... 320
`Visual C++ Version 2.1 (MFC 3.1) .......................................... 321
`Visual C++ Version 2.2 (MFC 3.2) .......................................... 321
`Visual C++ Version 4.0 (MFC 4.0) .......................................... 321
`Visual C++ Version 4.1 (MFC 4.1) .......................................... 321
`Visual C++ Version 4.2 (MFC 4.2) .......................................... 322
`Win32 versus Win 16 Control Development ................................... 322
`Extended Controls ........................................................ 322
`
`NFLE 1008 - Page 12
`
`
`
`xii + Contents
`
`Control-Specific Registry Entries .............................................. 323
`Control .............................................................. 323
`lnprocServer32 ........................................................ 324
`lnsertable ............................................................ 324
`MiscStatus ........................................................... 324
`ProglD .............................................................. 325
`ToolbarBitmap32 ...................................................... 325
`Typelib ............................................................. 326
`Version
`............................................................. 326
`Component Categories .................................................... 326
`Why Component Categories? ............................................. 326
`The CATID ........................................................... 327
`Categorizing Your Controls ............................................... 328
`The Component Categories Manager ........................................ 329
`ICatRegister .......................................................... 329
`ICatlnformation ........................................................ 332
`Summary .............................................................. 333
`
`Chapter 8: A Simple Control ••.•.•••••..••.••.••••••.•••.••••••• • 335
`Our First Control ......................................................... 335
`ControlWizard .......................................................... 336
`Activate When Visible ................................................... 338
`Invisible at Runtime ..................................................... 338
`Available in "Insert Object" Dialog ......................................... 338
`Has an "About" Box .................................................... 339
`Acts as Simple Frame Control ............................................. 339
`Which Window Class, If Any, Should This Control Subclass? ....................... 339
`Advanced ............................................................. 339
`Edit Names... . ........................................................ 340
`ControlWizard-Generated Files .............................................. 342
`COleControlModule ...................................................... 342
`AFX_MANAGE_STATE .................................................. 346
`COleControl ............................................................ 346
`Control Licensing ......................................................... 350
`
`NFLE 1008 - Page 13
`
`
`
`Contents + xiii
`
`COleObjectfactoryEx ................................................... 351
`Drawing the Control ...................................................... 354
`Registering the Control
`.................................................... 355
`Testing the Control ........................................................ 356
`Modifying the Default Toolbar Bitmap .......................................... 357
`Adding Stock Properties .................................................... 357
`Appearance .......................................................... 359
`BackColor ........................................................... 359
`ForeColor ............................................................ 360
`Caption or Text ........................................................ 360
`BorderStyle ........................................................... 360
`Font ................................................................ 361
`Hwnd ............................................................... 361
`Enabled .............................................................. 361
`Testing Stock Properties in the Test Container .................................... 363
`COlePropertyPage ..................................................... 363
`Modifying the Custom Property Page .......................................... 366
`Using Stock Property Pages ................................................. 372
`Using Ambient Properties ................................................... 373
`CFontHolder ............................................................ 375
`Testing the Ambient Property Changes ......................................... 376
`Adding a Stock Event ..................................................... 377
`Adding the Stock Methods .................................................. 379
`Adding a Custom Method .................................................. 379
`Adding a Custom Event .................................................... 380
`Serializing the Properties of a Control .......................................... 381
`Testing the Final Control in a Real Container ..................................... 383
`Adding Component Category Support ......................................... 386
`Debugging the Control .................................................... 391
`Summary .............................................................. 391
`
`Chapter 9: Graphical Controls •••••••••••••••••••••••••••••••••••• 393
`A Clock Control
`......................................................... 393
`MFC' s Drawing Classes .................................................... 395
`
`NFLE 1008 - Page 14
`
`
`
`xiv + Contents
`
`The CDC Class ........................................................ 395
`The CBrush Class ...................................................... 398
`The CPen Class ........................................................ 398
`The CFont Class ....................................................... 399
`The CBitmap Class ..................................................... 399
`Drawing the Clock ........................................................ 400
`Drawing the Tick Marks or Calculating the Tick Mark Points
`....................... 400
`rcBounds Upper Left Isn't at (0, O)
`.......................................... 402
`Drawing the Clock Hands ................................................ 402
`Drawing the Clock's Tick Marks and Hands ................................... 405
`Getting the Current Time ................................................. 406
`Mapping Modes ....................................................... 406
`The OnDraw Source ...................................................... 410
`Redrawing the Clock Every Second
`........................................... 412
`AmbientUIDead ....................................................... 415
`Testing the Clock ......................................................... 415
`Restricting the Size or Shape of the Control
`................................... 416
`Calculating HIMETRIC Units ............................................... 417
`Eliminating Control Flicker .................................................. 419
`rclnvalid ............................................................. 424
`Metafiles ............................................................... 425
`OnDrawMetafile ....................................................... 426
`Metafile Restrictions ..................................................... 426
`Win32 Enhanced Metafiles ............................................... 427
`Testing the Metafile ....................................................... 428
`Drawing the Control in Design Mode .......................................... 428
`Hiding Properties ...................................................... 430
`The SecondChange Event .................................................. 431
`The Date Property
`. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
`COleDateTime ........................................................ 431
`Property Pages .......................................................... 432
`MFC Control Container Support .............................................. 433
`The CWnd Class ....................................................... 433
`An Example ............................................................ 434
`
`NFLE 1008 - Page 15
`
`
`
`Contents -+ xv
`
`.............................................................. 441
`Events
`Dynamic Creation ...................................................... 443
`Summary .............................................................. 449
`
`Chapter 10: Subdassing Windows Controls •••••••••••••••••••••••••• 453
`Subclassing a Windows Control .............................................. 453
`The Expression Class Again ................................................. 455
`Creating the EEdit Project ................................................... 455
`Code Added by ControlWizard .............................................. 457
`The Windows Edit Control .................................................. 460
`Window Style Bits ........................................................ 461
`Changing a Window's Style Bits before Window Creation ........................ 462
`Changing a Window's Style Bits at Run Time .................................. 463
`OleControl:: RecreateControlWindow ........................................ 463
`Modifying Control Behavior with Messages ...................................... 466
`Added Expression Capabilities with ActiveX Controls .............................. 468
`Adding the Stock Events .................................................... 468
`Reflected Window Messages ................................................ 468
`Handling Reflected Messages ................................................ 470
`Processing a Control's Notification Messages .................................... 471
`Setting the Colors of a Subdassed Control ...................................... 473
`WM_CTLCOLOR and Win32 ............................................. 476
`Some Problems with Control Subclassing ....................................... 478
`Setting Default Values for Your Control's States ................................... 480
`In the Control's Constructor ............................................... 480
`In the Control's DoPropertyExchange Method .................................. 480
`In the Control's OnResetState Method ........................................ 481
`Adding the Expression Functionality ........................................... 482
`How to Handle an Invalid Entry Condition
`...................................... 484
`Enumerating Property Values .............................................. 488
`Property Pages Revisited ................................................... 490
`Using the Control
`........................................................ 494
`Drawing Your Controls the 3-D Windows 95 Way ................................ 495
`Subdassing Windows 95 Common Controls ..................................... 496
`
`NFLE 1008 - Page 16
`
`
`
`xvi + Contents
`
`Subclassing the Tree View Control ............................................ 497
`Using the MFC Control Classes ............................................ 498
`The Properly Page ........................................................ 509
`Summary .............................................................. 510
`
`Chapter 11: Nonvisual Controls ••••••••••••••••••••••••••••••••••• 511
`Goals of Nonvisual Controls ................................................ 511
`A Win32 Pipe Control ..................................................... 511
`Named Pipes ......................................................... 512
`Message Types ........................................................ 512
`Asynchronous versus Synchronous 1/0 ...................................... 512
`Pipe Names .......................................................... 513
`Creating the Pipe Control Project ............................................. 514
`Drawing the Control during the Design Phase .................................... 514
`CPictureHolder ........................................................ 516
`OnSetExtent .......................................................... 517
`Adding the Pipe Functionality ................................................ 517
`Adding the Properties ..................................................... 519
`ErrorMsg ............................................................ 519
`PipeName ........................................................... 521
`Pipe Type ............................................................ 521
`Adding the Pipe Methods ................................................... 525
`Create .............................................................. 525
`Destroy .............................................................. 527
`Connect ............................................................. 527
`Disconnect ........................................................... 529
`W