Mastering Delphi 3
Table of Content
This is the complete Table of Contents of the final version of the book Mastering Delphi 3.
Page numbers have been left in (although they are not properly aligned) to let you evaluate
how much space is devoted to each topic. New sections or sections heavily revised
(compared to the last edition) are in bold face. Some of these topics are Delphi 3 specific,
but not all of them.
Keep in mind, anyway, that the entire books has been revised with extreme care, resulting in
countless changes and corrections, and many new tips, notes, and sidebars. There are over
50 brand new examples. And the entire source code is now available in HTML format.
PART I: Delphi and Object Pascal 1
Chapter 1 A Form Is a Window (and an ActiveX) 3
Creating Your First Form 4
Adding a Title 5
Saving the Form 7
Using Components 7
Changing Properties 9
Responding to Events 11
Compiling and Running a Program 15
Changing Properties at Run-Time 18
Adding Code to the Program 19
A Two-Way Tool 21
Looking at the Source Code 22
The Textual Description of the Form 24
The Project File 27
Building an ActiveForm 28
An ActiveX Library and an Active Form 30
The HTML Page 32
What's Next 34
Chapter 2 Highlights of the Delphi Environment 37
Different Versions of Delphi 38
Asking for Help 39
Delphi Menus and Commands 41
The File Menu 42
The Edit Menu 43
The Search Menu 47
The View Menu 49
The Project Menu 49
The Run Menu 51
The Component Menu 51
The Database Menu 52
The Tools Menu 53
The Help Menu 53
The Delphi Toolbar 54
The Local Menus 55
Working with the Form Designer 55
The Component Palette 59
The Object Inspector 60
The Alignment Palette 60
Writing Code in the Editor 61
Using Editor Bookmarks 62
Delphi 3 Code Insight 63
Code Completion 63
Code Templates 64
Code Parameter 65
Managing Projects 66
The Project Manager 66
Setting Project Options 67
Compiling a Project 68
Exploring a Compiled Program 69
The Integrated Debugger 69
The Object Browser 70
Additional Delphi Tools 70
The Files Produced by the System 71
What's Next 76
Chapter 3 The Object Repository and the Delphi Wizards 79
The Object Repository 80
The New Page 82
The ActiveX Page 83
The Current Project Page 84
The Forms Page 84
The Dialogs Page 85
The Data Modules Page 86
The Projects Page 86
Delphi Wizards 88
The Database Form Wizard 88
The Application Wizard 91
The Dialog Wizard 92
Customizing the Object Repository 93
Adding New Application Templates 93
Adding New Form Templates to the Object Repository 95
The Object Repository Options 96
What's Next 98
Chapter 4 The Pascal Language 101
Types, Variables, and Constants 102
Variables 103
Constants 104
Resource String Constants 105
Delphi Data Types 106
Ordinal Types 106
Real Types 110
Date and Time 112
Specific Windows Types 115
Typecasting and Type Conversions 118
The Variant Type 120
Variants in Depth 122
Variants Are Slow! 122
User-Defined Data Types 123
Subrange Types 125
Enumerated Types 126
Set Types 128
Array Types 130
Record Types 132
Pointers 134
File Types 136
Strings in Delphi 137
Traditional Turbo Pascal Strings 137
Delphi Long Strings 138
C-Like Character Arrays 141
String Conversions 142
String Conversion Blues 144
Formatting Strings 145
Coding Style 148
Comments 148
Use of Uppercase 149
White Space 149
Pretty-Printing 150
Syntax Highlighting 152
Language Statements 154
Expressions and Operators 154
Simple and Compound Statements 156
Conditional Statements 157
Loops in Pascal 160
The With Statement 164
Procedures and Functions 167
Reference Parameters 168
Constant Parameters 169
Open Array Parameters 170
Type-Variant Open Array Parameters 171
Delphi Calling Conventions 174
What Is a Method? 174
Forward Declarations 174
External Declarations 176
Procedural Types 177
A Windows Callback Function 180
What's Next 182
Chapter 5 Object Pascal as an OOP Language 185
Introducing Classes and Objects 186
Delphi's Object Reference Model 189
Using the TDate Class in Delphi 191
Declaring a Constructor 193
Looking at Objects in Memory 195
Classes and Information Hiding 197
Private, Protected, and Public 198
Public and Published 199
Classes and Units 200
The Interface of a Class 205
Units and Scope 205
Units and Name Clashes 207
Encapsulating Changes 208
A Unit for the TDate Class 209
Units and Programs 214
Inheriting from Existing Types 215
Inheritance and Type Compatibility 219
Late Binding and Polymorphism 223
Overriding and Redefining Methods 226
Virtual versus Dynamic Methods 227
Message Handlers 228
Abstract Methods 229
Run-Time Type Information 232
Handling Exceptions 235
An Example of the Use of Exceptions 237
The Finally Block 243
What's Next? 246
Chapter 6 Advanced Object Pascal 249
The Self Keyword 250
Creating Components Dynamically 251
Class Methods and Class Data 253
A Class with an Object Counter 254
Method Pointers 258
The Updated Counter Example 260
Class References 265
Creating Components at Run-Time Using Class References 267
Interface and Multiple Inheritance 270
Declaring an Interface 270
Using an Interface as Parameter 274
Writing a Second Interface 274
Implementing Both Interfaces 276
Defining Properties 280
Adding Properties to the TDate Class 283
Events in Delphi 287
Events Are Properties 288
Adding an Event to the TDate Class 288
Creating a TDate Component 291
What's Next 293
Chapter 7 The Visual Component Library 295
The TObject Class 296
Showing Class Information 298
The VCL Hierarchy 300
Components 301
Objects 305
Exceptions 307
Using VCL Classes 308
Common VCL Properties 310
The Name Property 312
Properties Related to Component Size and Position 315
Activation and Visibility Properties 316
The Customizable Tag Property 317
The User Interface: Color and Font 318
Common VCL Methods 320
Common VCL Events 322
Using Delphi Collections 324
Using Lists of Objects and Data 325
Studying the VCL Source Code 328
What's Next 328
PART II Using Components 331
Chapter 8 A Tour of the Basic Components 333
Windows' Own Components 334
Clicking a Button 336
The Buttons Example 336
Clicking the Mouse Button 340
Adding Colored Text to a Form 341
The LabelCo Example 342
Dragging from One Component to Another 346
The Code for the Dragging Example 348
Accepting Input from the User 349
Handling the Input Focus 349
A Generic OnEnter Event Handler 353
Entering Numbers 353
Sophisticated Input Schemes 358
Creating a Simple Editor 361
The Font Dialog Box 361
Creating a Rich Editor 363
Making Choices 364
Grouping Radio Buttons 366
The Phrases1 Example 367
A List with Many Choices 370
The Form of the Phrases2 Example 371
Working with the List Boxes 373
Removing a Selected String from the Other List Box 374
Allowing Multiple Selections 379
The Third Version of the Phrases Example 380
Using a CheckListBox Component 384
Many Lists, Little Space 385
Choosing a Value in a Range 387
The Scroll Color Example 388
What's Next 391
Chapter 9 Creating and Handling Menus 393
The Structure of the Main Menu 394
Different Roles of Menu Items 396
Building a Menu with the Menu Designer 396
The Standard Structure of a Menu 397
Shortcut Keys and Hotkeys 397
Using the Predefined Menu Templates 398
Responding to Menu Commands 399
The Code Generated by the Menu Designer 400
The Code of the MenuOne Example 403
Modifying the Menu at Run-Time 405
Changing Menu Items at Run-Time 405
Disabling Menu Items and Hiding Pull-Down Menus 407
Using Radio Menu Items 409
Creating Menu Items Dynamically 410
Short and Long Menus 416
Graphical Menu Items 417
Customizing the Menu Check Mark 417
Bitmap Menu Items 421
Owner-Draw Menu Items 426
Customizing the System Menu 430
Building a Complete Menu 434
The File Menu 435
The Paragraph Menu 441
The Font Menu 443
The Options Menu 446
Pop-Up Menus 447
An Automatic Local Menu 447
Modifying a Pop-Up Menu When It Is Activated 449
Handling Pop-Up Menus Manually 450
What's Next 452
Chapter 10 Back to the Form 455
Forms versus Windows 456
Overlapped, Pop-Up, and Child Windows 458
The Application Is a Window 460
Setting Form Styles 461
Creating Topmost Forms 462
Avoiding Topmost Flickering 463
The Border Style 465
The Effect of the Border Style Property 466
The Borders Example 467
The Border Icons 469
Setting More Windows Styles 471
Manual Form Scaling 473
Automatic Form Scaling 476
Setting the Form's Position and Size 477
Minimizing and Maximizing a Form 479
The Size of a Form and Its Client Area 481
The Maximum and Minimum Size of a Form 482
Automatic Form Creation 484
Closing a Form 486
Supervising Keyboard Input 488
Getting Mouse Input 491
The Mouse Buttons 492
Using Windows without a Mouse 492
The Parameters of the Mouse Events 493
Click and Draw: The Shapes1 Example 493
Drawing on the Form 496
The Drawing Tools 497
Drawing Shapes 498
Drawing and Painting in Windows 501
Painting a Single Shape 503
Painting a List of Shapes 504
Delphi Output Components 509
What's Next 510
Chapter 11 Graphical Components 513
Improving the User Interface with Graphics 514
A Bitmap in a Button 515
A Car in a Button 516
An Animated Bitmap in a Button 522
A Two-State Button 522
Many Images in a Bitmap 524
The Rotating World 526
A List of Bitmaps, the Use of Resources, and a PaintBox 529
The Animate Control 534
An Image Viewer 538
Drawing in a Bitmap 541
Drawing Shapes 541
Graphical Lists 545
Drawing a List of Colors 545
The Outline of the Book 549
A Tree of Chapters 554
The Nodes of the Outline 557
A Graphical List 561
Graphical Grids 563
A Grid of Fonts 565
Mines in a Grid 570
Choosing Colors 576
Using TeeChart 577
Building a First Example 579
Adding Data to the Chart 581
Creating Series Dynamically 583
The DateTimePicker New Common Control 586
What's Next 587
Chapter 12 A Toolbar, a CoolBar, and a Status Bar 589
Grouping Controls with Panels 590
Building a Toolbar with a Panel 591
A First Toolbar 592
Enabling and Disabling Toolbar Buttons 595
Adding Hints to the Toolbar 597
Adding Customized Hints to a Form 600
Customizing Hints 600
Multiple Hints for a Control 603
Adding Features to a Toolbar 604
A Combo Box in a Toolbar 604
A Toolbar You Can Drag 606
The Toolbar Windows 95 Control 611
A Really Cool Toolbar 615
Creating a Status Bar 618
Menu Hints in the Status Bar 619
Speed Button Hints in the Status Bar 621
What's Next? 623
Chapter 13 Multiple Forms and Dialog Boxes 625
Dialog Boxes versus Forms 626
Adding a Second Form to a Program 627
Modal and Modeless Forms 630
Two Forms, Two Menus 632
Merging Form Menus 634
Creating a Dialog Box 636
Modal Dialog Boxes 637
Closing a Dialog Box 640
A Modeless Dialog Box 640
Using Predefined Dialog Boxes 646
Windows Common Dialogs 646
A Parade of Message Boxes 652
Extensible Dialog Boxes 656
Including About Boxes 660
Using the System About Box 660
Building a Custom Hidden Screen 661
Building a Splash Screen 664
Visual Form Inheritance 669
Inheriting from a Base Form 670
Polymorphic Forms 673
What's Next 679
Chapter 14 Scrolling, Multipage Forms, and Splitting 681
When Forms Are Too Big 682
Scrolling a Form 683
The Scroll Testing Example 684
Automatic Scrolling 687
Scrolling an Image 687
Scrolling and Form Coordinates 689
Building Notebooks with Delphi 691
PageControls and TabSheets 692
A Notebook with a Tab Set 697
Changing the Page of a Notebook 702
Tabbed Notebooks 705
Notebooks without Tabs and Tabs without Notebooks 706
A Presentation in a Notebook 707
An Image Viewer with Tabs 708
An Image Browser with Tabs 711
A Multipage Toolbar 712
Form Splitting Techniques 714
Splitting with a Splitter 714
Horizontal Splitting 717
Alternative Splitting Components 718
Splitting with a Header 719
Splitting with Panels 722
Drawing a Split Line 725
Direct Mouse Handling in a Form 729
Dragging and Clipping the Mouse 730
The Dragging Code 731
What's Next 733
Chapter 15 Creating MDI Applications 735
MDI in Windows: A Technical Overview 736
Frame and Child Windows in Delphi 739
A First Delphi MDI Demo 740
Building a Complete Window Menu 742
Building a Child Window 744
MDI Applications with Different Child Windows 747
Adding a Bouncing Shape 747
The Menu of the New Child Form 749
Changing the Main Form 750
A Fast Start with MDI 752
What's Next 754
Chapter 16 Building Database Applications 757
Data, Files, Databases, and Tables 758
What Is a Table? 760
Operations on Database Data 762
Delphi Database Components 762
Tables and Queries 764
The Status of a Data Set 765
Other Data-Access Components 766
Delphi Data-Aware Controls 767
Building Database Applications by Hand 769
A Database Grid 769
Customizing the DBGrid 771
The Table State 772
Using DBEdit Controls 773
Using a Query 775
A Query with Parameters 779
Using the Database Form Wizard 781
Accessing the Data Fields 782
The Hierarchy of Field Classes 785
Adding a Calculated Field 788
Using Fields to Manipulate a Table 793
Looking for Records in a Table 793
The Total of a Table Column 798
Editing a Table Column 801
Exploring the Tables of a Database 802
Choosing a Database and a Table at Run-Time 803
A Table Viewer 805
A Field Editor 808
A Better User Interface for the Table Browser 809
Creating a Table 810
Creating Tables Dynamically 812
The Form and Its Startup Code 813
Creating a New Table 814
Choosing an Existing Table with the Proper Fields 818
Adding or Removing Records 820
A Multi-Record Grid 821
Moving Control Grid Panels 823
Building a Master Detail Form with the Wizard 824
A Master Detail Structure with Queries 827
Providing a Closed Selection in a Combo Box 827
A Lookup in a Grid 829
What's Next? 831
Chapter 17 Advanced Database Access 833
Accessing a SQL Server 834
A First InterBase Application 835
Accessing a Remote SQL Server 837
InterBase Server Tools 837
Moving an Existing Program to the SQL Server 840
Copying a Table 841
Porting the Application 843
Using the Visual Query Builder 844
From Porting to Upsizing 847
Joining Tables with the Visual Query Builder 848
A Join with Three Tables 848
A Join with More Tables 851
Data Modules 852
Creating a Data Module 853
A Data Module for Multiple Views 855
Setting Field Properties and Initial Values 857
Standard Table Filtering 859
Custom Table Filtering 860
Custom Filtering and Client/Server Development 863
The Data Dictionary 864
The Data Dictionary and the Fields Editor 865
What's in an Attribute Set? 866
Exploring the Data Dictionary 868
Database Transactions 869
A Simple Example of Transactions 870
Custom Database Login 872
Handling Database Errors 873
What's Next? 878
PART III Components and Libraries 881
Chapter 18 Creating Components 883
Extending the VCL 884
From Components to Packages 885
Rules for Writing Components 887
Building Your First Components 888
The Fonts Combo Box 889
Creating a Package 892
Using the Fonts Combo Box 896
Creating a Tabbed List Box 897
Testing the Tab List Component 898
Building Brand-New Components 900
A Graphical Component Built Step-by-Step 900
Defining an Enumerated Property 901
Writing the Paint Method 904
The Test Program 905
Adding Class-Typed Properties 907
Updating the Test Program 911
Defining a New Custom Event 912
Testing the OnArrowDblClick Event 915
Adding a Bitmap for the Components Palette 916
The Final Test of the Arrow Component 917
Correcting Bugs in the Arrow Component 918
Arrows and Shapes 920
The Clock Component 922
Defining an Array Property 924
The New Tabbed List 925
Using Dialog Box Units 928
A Header and a Tabbed List Box 930
Building a Nonvisual Component 932
A Dialog Box in a Component 933
Using the Nonvisual Component 937
What's Next 938
Chapter 19 Components and the ToolsAPI 941
Writing a Property Editor 942
Types of Property Editors 942
An Editor for the TabsString Property 944
Installing the Property Editor 947
Writing a Component Editor 948
Subclassing the TComponentEditor Class 949
A Component Editor for the Tabbed List 949
Registering the Component Editor 952
Writing a Simple Wizard 952
Subclassing the TIExpert Class 953
Writing a 'Stupid' Wizard 954
Other Interfaces of the ToolsAPI 956
Accessing Properties by Name 958
What's Next 961
Chapter 20 Dynamic Link Libraries 963
The Role of DLLs in Windows 964
What Is Dynamic Linking? 964
What Are DLLs For? 966
Understanding System DLLs 968
Using Packages Statically or Dynamically 969
Differences between DLLs and EXEs 970
Rules for DLL Writers 970
Win16 and Win32 DLLs 971
Using Existing DLLs 971
Building the C++ DLL 972
Declaring the C++ DLL Functions in Delphi 973
Creating a DLL in Delphi 977
A First Simple Delphi DLL 977
Calling the Delphi DLL 979
A Delphi Form in a DLL 980
Calling the DLL Form from Delphi 983
Calling a Delphi DLL from Visual Basic for Applications 984
Building a DLL of Icons 985
Loading the Icons from the DLL 986
Calling a DLL Function at Run-Time 987
A DLL in Memory 990
What's Next 992
Chapter 21 OLE and COM 995
Freely available on the Web at my site....
What Is OLE? And What Is COM? 996
Objects in DLLs 998
Writing the Class in the DLL 998
Using a Class from a DLL 1000
Implementing IUnknown 1003
Implementing IUnknown Methods 1004
Global Unique Identifiers 1005
The Role of Class Factories 1008
Using Delphi 3 COM Interfaces 1009
The TComObject Class 1010
Initializing the COM Object 1012
Updating the COM Client Program 1015
Using Interface Properties 1017
Using a Shell Interface 1019
Creating a Shell Link (or Shortcut) 1019
A Copy Hook for Pascal Files 1021
What's Next 1025
Chapter 22 OLE Automation and OLE Documents 1027
OLE Automation 1028
Sending Data to Word 1029
Sending Database Data to Microsoft Word 1030
Writing an OLE Automation Server 1033
Introducing Type Libraries 1033
The Type Library Editor 1035
Registering the Automation Server 1039
Writing a Client for Our Server 1039
What Is an OLE Document? 1042
Using the OleContainer Component 1044
A Minimal OLE Container 1045
Adding a Menu to the OLE Container Demo 1047
Visual Editing and Toolbars 1049
The OLE Standard Dialog Boxes 1050
Loading and Saving Objects in Files 1052
Multiple OLE Containers 1053
What's Next 1055
Chapter 23 Using and Creating ActiveX Controls 1057
Introducing ActiveX Controls 1058
ActiveX Controls versus Delphi Components 1060
Using ActiveX Controls in Delphi 1061
Installing an ActiveX Control 1061
The TOleControl Class 1063
Using ActiveX Controls 1063
Building a Chart 1065
Writing ActiveX Controls 1067
Building an ActiveX Arrow 1067
Adding New Properties 1072
Adding a Property Page 1074
What's Next 1078
Chapter 24 Internet Programming 1081
Browsing HTML files 1082
Generating HTML Files 1087
Building a Plain HTML File 1088
Producing HTML Tables 1092
Handling Live Data 1095
Building ActiveForms 1096
The Role of an ActiveX Form on a Web Page 1101
A Multi-Page ActiveForm 1103
What's Next 1103
PART IV Advanced Delphi Programming 1107
Chapter 25 Discovering the Application Structure 1109
Using the Application Object 1110
Showing the Application Window 1113
The Application System Menu 1114
Activating Applications and Forms 1116
Checking for Multiple Instances of an Application 1118
Looking for a Copy of the Main Window 1118
Activating the Previous Main Form 1119
Handling User-Defined Windows Messages 1121
Searching the Windows List 1122
Using a Mutex 1124
Programming without Components 1125
The Smallest Delphi Program? 1125
Reading the Command Line 1126
Events, Messages, and Multitasking in Windows 1128
Event-Driven Programming 1128
Windows Message Delivery 1130
Building a Clock with a Timer 1131
A Graphical Clock 1132
Painting the Seconds Hand with Raster Operations 1136
Idle Computing and Multitasking 1139
Background Processing 1139
Computing Prime Numbers (the Dumb Way) 1140
Multithreading in Delphi 1146
The TThread Class 1147
A First Example 1148
A Locking Example 1150
Synchronization Alternatives 1151
Thread Priorities 1152
Synchronizing Threads 1154
Waiting for a Thread 1154
Windows Synchronization Techniques 1159
Using the Screen Object 1164
Getting Screen Information 1165
Handling the Forms List 1166
Saving Status Information 1169
Using Windows INI Files 1169
Using the Registry 1173
What's Next? 1179
Chapter 26 Exploring the Behavior of a Program 1181
Using the Debugger 1182
Debug Information 1183
Setting Breakpoints 1184
Inspecting Values 1190
More on Breakpoints 1192
Tracing through the VCL Source Code 1194
Alternative Debugging Techniques 1195
Debugging with a Terminal Window 1196
Debug and Release Versions (using Conditional Compilation) 1197
Using Assertions 1199
Viewing a Compiled Program with the Object Browser 1201
Exploring the Message Flow 1203
Using WinSight 1204
A Look at Posted Messages 1208
The Memory Image of an Application 1213
Windows System Memory 1215
Free System Memory 1216
What's Next? 1218
Chapter 27 Using Resources 1221
Resources in Windows 1222
Using Resource Editors 1223
Loading an Icon or Bitmap as a Property 1226
The Manual Approach to Loading Resources 1227
The Icons for Applications and Forms 1229
Adding Alternative Icons for Different Conditions 1230
Using the Icon Tray of the Taskbar 1232
Showing the Memory Status with a Tray Icon 1234
Hiding and Showing the Main Window 1236
Hiding the Taskbar Icon 1238
Using the Cursor in Delphi 1240
Designing a Custom Cursor 1241
A Flexible Cursor 1244
Using String Table Resources 1247
Translating the Strings into Another Language 1252
Version Information 1254
What's Next? 1257
Chapter 28 Adding Printing Capabilities to Delphi Applications 1259
Printing a Whole Form 1260
A Custom Print Dialog Box 1261
The Standard Print Dialog Boxes 1262
Accessing the Printer Object 1265
A Print Preview of Graphics 1265
Sharing the Output Code 1270
Printing Text 1272
Printing Database Records and Tables 1275
The QuickReport Components 1278
A Quick Example 1279
Creating ReportSmith Reports 1282
Building a Custom Report 1284
Writing a ReportSmith Macro 1287
Quicker than ReportSmith 1290
What's Next 1292
Chapter 29 Adding File Support to Applications 1295
Files and the Pascal Language 1296
Handling Text Files 1298
A Text File Converter 1302
Saving Generic Data 1305
File Support in Delphi Components 1310
File System Components 1310
Streaming Data 1313
Streaming Numbers 1315
Streaming Components 1317
What's Next 1321
Chapter 30 Exchanging Data 1323
What Is the Clipboard? 1324
The Clipboard in Delphi 1326
Copying and Pasting Text 1326
Copying and Pasting Bitmaps 1329
Copying Delphi Components to the Clipboard 1332
Copying Custom Data to the Clipboard 1335
Dynamic Data Exchange (DDE): A Technical Overview 1340
DDE Conversations 1340
Roles of the Server and Client 1340
DDE in Delphi 1342
A Simple Example of DDE 1342
Copying and Pasting DDE Links 1346
Copying Link Data to the Clipboard 1346
DDE with Timers and Graphics 1347
The Automatic Server 1348
A Graphical DDE Client 1348
What's Next 1351
Chapter 31 Multimedia Fun 1353
Windows Default Sounds 1354
Every Box Has a Beep 1355
From Beeps to Music 1359
Adding Default Sounds to Delphi Applications 1361
The Media Player Component 1362
Playing Sound Files 1363
Running Videos 1364
A Video in a Form 1366
Working with a CD Drive 1368
Epilogue 1372
APPENDIXES
Appendix A Quick Overview of OOP Concepts 1375
Abstraction in Programming Languages 1376
Classes 1377
Inheritance 1378
Polymorphism 1378
A Definition of OOP 1379
OOP Languages 1380
Pure versus Hybrid OOP 1380
Static versus Dynamic Type Checking 1380
The Object Model: Traditional versus Reference 1380
Object-Oriented Analysis and Design 1381
Appendix B An Introduction to SQL 1383
What Is SQL? 1384
The Select Statement 1384
Avoiding Duplicates 1386
Making a Join 1386
Choosing an Order 1387
Computing Values 1387
Defining Groups 1387
Beyond Select 1388
Index 1389