Mastering Delphi 7 by Marco Cantu ISBN:078214201X Sybex © 2003 (1011 pages) The best Delphi resource now updated and expanded. Companion Web Site Table of Contents Mastering Delphi 7 Introduction Part I - Foundations Ch apt er 1 - Delphi 7 and Its IDE Ch apt er 2 - The Delphi Programming Language Ch apt er 3 - The Run-Time Library Ch apt er 4 - Core Library Classes Ch apt er 5 - Visual Controls Ch apt er 6 - Building the User Interface Ch apt er 7 - Working with Forms Part II - Delphi Object-Oriented Architectures Ch apt er 8 - The Architecture of Delphi Applications Ch apt er 9 - Writing Delphi Components Ch apt er 10 - Libraries and Packages Ch apt er 11 - Modeling and OOP Programming (with ModelMaker) Ch apt er 12 - From COM to COM+ Part III - Delphi Database-Oriented Architectures Ch apt er 13 - Delphi's Database Architecture Ch apt er 14 - Client/Server with dbExpress Ch apt er 15 - Working with ADO Ch apt er 16 - Multitier DataSnap Applications Ch apt er 17 - Writing Database Components Ch apt er 18 - Reporting with Rave Part IV - Delphi, the Internet, and a .NET Preview Ch apt er 19 - Internet Programming: Sockets and Indy Ch apt er 20 - Web Programming with WebBroker and WebSnap Ch apt er 21 - Web Programming with IntraWeb Ch apt er 22 - Using XML Technologies Ch apt er 23 - Web Services and SOAP Ch apt er 24 - The Microsoft .NET Architecture from the Delphi Perspective Ch apt er 25 - Delphi for .NET Preview: The Language and the RTL Ap pe ndi x A - Extra Delphi Tools by the Author Ap pe ndi x B - Extra Delphi Tools from Other Sources Ap pe ndi x C - Free Companion Books on Delphi Index List of Figures List of Tables List of Listings List of Sidebars Back Cover Whether you're new to Delphi or just making the move from an earlier version, Mastering Delphi 7 is the one resource you can't do without. Practical, tutorial-based coverage helps you master essential techniques in database, client-server, and Internet programming. And the insights of renowned authority Marco Cantú give you the necessary knowledge to take advantage of what's new to Delphi 7 particularly its support for .NET. Coverage includes: • Creating visual web applications with IntraWeb • Writing sockets-based applications with Indy • Creating data-aware controls and custom dataset components • Creating database applications using ClientDataSet and dbExpress • Building client-server applications using InterBase • Interfacing with Microsoft's ADO • Programming for a multi-tiered application architecture • Taking advantage of Delphi's support for COM, OLE Automation, and COM+ • Taking advantage of Delphi's XML and SOAP support • Implementing Internet protocols in your Delphi app • Creating UML class diagrams using ModelMaker • Visually preparing reports using RAVE • Using the Delphi language to create your first .NET programs About the Author Marco Cantú is an internationally known programming author and teacher who specialize in Delphi development and XML-related technologies. Author of the best-selling Mastering Delphi series, he teaches advanced Delphi classes, speaks at conferences worldwide, and writes about Delphi programming in print and online magazines. Mastering Delphi 7 Marco Cantù Associate Publisher: Joel Fugazzotto Acquisitions Editor: Denise Santoro Lincoln Developmental Editor: Brianne Agatep Production Editor: Kelly Winquist Technical Editor: Brian Long Copyeditor: Tiffany Taylor Compositor: Rozi Harris, Interactive Composition Corporation Proofreaders: Nancy Riddiough, Emily Hsuan, Leslie Higbee Light, Monique Vandenberg, Laurie O'Connell, Eric Lach Indexer: Ted Lau Book Designer: Maureen Forys, Happenstance Type-O-Rama Cover Designer: Design Site Cover Illustrator: Tania Kac, Design Site Copyright © 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. The author(s) created reusable code in this publication expressly for reuse by readers. Sybex grants readers limited permission to reuse the code found in this publication so long as the author(s) are attributed in any application containing the reusable code and the code itself is never distributed, posted online by electronic transmission, sold, or commercially exploited as a stand-alone product. Aside from this specific exception concerning reusable code, no part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic, or other record, without the prior agreement and written permission of the publisher. Library of Congress Card Number: 2002115474 ISBN: 0-7821-4201-X SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries. Mastering is a trademark of SYBEX Inc. Screen reproductions produced with Collage Complete. Collage Complete is a trademark of Inner Media Inc. TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. The author and publisher have made their best efforts to prepare this book, and the content is based upon final release software whenever possible. Portions of the manuscript may be based upon pre-release versions supplied by software manufacturer(s). The author and the publisher make no representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of any kind including but not limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused or alleged to be caused directly or indirectly from this book. Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 To the late Andrea Gnesutta, a friend the Italian Delphi community prematurely lost. Acknowledgments This seventh edition of Mastering Delphi follows the seventh release of a Delphi development environment by Borland, a revolution started in the winter of year 1994. As it has for many other programmers, Delphi (and its Linux twin, Kylix) has been my primary interest throughout these years; and writing, consulting, teaching, and speaking at conferences about Delphi have absorbed more and more of my time, leaving other languages and programming tools in the dust of my office. Because my work and my life are quite intertwined, many people have been involved in both, and I wish I had enough space and time to thank them all as they deserve. Instead, I'll just mention a few particular people and say a warm "Thank You" to the entire Delphi community (especially for the Spirit of Delphi 1999 Award I've been happy to share with Bob Swart). The first official thanks are for the Borland programmers and managers who made Delphi possible and continue to improve it: Chuck Jazdzewski, Danny Thorpe, Eddie Churchill, Allen Bauer, Steve Todd, Mark Edington, Jim Tierney, Ravi Kumar, Jörg Weingarten, Anders Ohlsson, and all the others I have not had a chance to meet. I'd also like to give particular thanks to my friends John Kaster and David Intersimone (at Borland's Developer Relations), and others who have worked at Borland, including Charlie Calvert and Zack Urlocker. The next thanks are for the Sybex editorial and production crew, many of whom I don't even know. Special thanks go to Brianne Agatep, Denise Santoro Lincoln, Tiffany Taylor, Rozi Harris, and Kelly Winquist; I'd also like to thank Joel Fugazzotto and Monica Baum. This edition of Mastering Delphi has had a very detailed and scrupulous review from Delphi guru Brian Long ( www.blong.com). His highlights and comments have improved the book in all areas: technical content, accuracy, examples, and even readability and grammar! Thanks a lot. In writing this book I had special contributions (to different extents) to the chapters on add-on tools and in the area of .NET programming from (in alphabetical order) John Bushakra, Jim Gunkel, Chad Hower, and Robert Leahey. A short bio and contact information for each of them is in the chapters they helped me write. Previous editions also had special contributions: Tim Gooch worked on Mastering Delphi 4 and Giuseppe Madaffari contributed database material for the Delphi 5 edition. For Mastering Delphi 6, Guy Smith-Ferrier rewrote the chapter on ADO and Nando Dessena helped me with the InterBase material. Many improvements to the text and sample programs were suggested by technical reviewers of past editions (Delphi R&D team member Danny Thorpe, Juancarlo Añez, Ralph Friedman, Tim Gooch, and Alain Tadros) and in other reviews over the years by Bob Swart, Giuseppe Madaffari, and Steve Tendon. Uberto Barbini helped me write Mastering Kylix 2 and some of his ideas ended up also affecting this book. Special thanks go to my friends Bruce Eckel, Andrea Provaglio, Norm McIntosh, Johanna and Phil of the BUG-UK, Ray Konopka, Mark Miller, Cary Jensen, Chris Frizelle of The Delphi Magazine, Mike Orriss, Dan Miser, my co-worker Paolo Rossi, and the entire Italian D&D Team (www.dedonline.com). Also, a very big "Thank You" to all the attendees of my Delphi programming courses, seminars, and conferences in Italy, the United States, France, the United Kingdom, Singapore, the Netherlands, Germany, Sweden . My biggest thanks go to my wife Lella who had to endure yet another book-writing session and too many late nights (after spending the evenings with our daughter, Benedetta I'll thank her with a hug, as Daddy's book looks quite boring to her). Many of our friends (and their kids) provided healthy breaks in the work: Sandro and Monica with Luca, Stefano and Elena, Marco and Laura with Matteo and Filippo, Bianca and Paolo, Luca and Elena with Tommaso, Chiara and Daniele with Leonardo and Matteo, Laura, Vito and Marika with Sofia. Our parents, brothers, sisters, and their families were very supportive, too. It was nice to spend some of our free time with them and our seven nephews Matteo, Andrea, Giacomo, Stefano, Andrea, Pietro, and Elena. Finally, I would like to thank all of the people, many of them unknown, who enjoy life and help to build a better world. If I never stop believing in the future and in peace, it is also because of them. Visit Marco's Delphi Developer Website This book's author, Marco Cantù, has created a site specifically for Delphi developers, at www.marcocantu.com. It's a great resource for all of your Delphi programming needs. The site includes: • The source code of the book • Extra examples and tips • Delphi components, wizards, and tools built by the author • The online books Essential Pascal, Essential Delphi, and others • Papers the author has written about Delphi, C++, and Java • Extensive links to Delphi-related websites and documents • Other material related to the author's books, the conferences he speaks at, and his training seminars The site also hosts a newsgroup, which has a specific section devoted to the author's books, so that readers can discuss the book content with him and among themselves. Other sections of the newsgroup discuss Delphi programming and general topics. The newsgroup can also be accessed from a Web interface. Introduction The first time Zack Urlocker showed me a yet-to-be-released product code-named Delphi, I realized that it would change my work and the work of many other software developers. I used to struggle with C++ libraries for Windows, and Delphi was and still is the best combination of object-oriented programming and visual programming not only for this operating system but also for Linux and soon for .NET. Delphi 7 simply builds on this tradition and on the solid foundations of the VCL to deliver another astonishing and all-encompassing software development tool. Looking for database, client/server, multitier, intranet, or Internet solutions? Looking for control and power? Looking for fast productivity? With Delphi and the plethora of techniques and tips presented in this book, you'll be able to accomplish all this. Seven Versions and Counting Some of the original Delphi features that attracted me were its form-based and object-oriented approach, its extremely fast compiler, its great database support, its close integration with Windows programming, and its component technology. But the most important element was the Object Pascal language, which is the foundation of everything else. Delphi 2 was even better! Among its most important additions were these: the Multi-Record Object and the improved database grid, OLE Automation support and the variant data type, full Windows 95 support and integration, the long string data type, and Visual Form Inheritance. Delphi 3 added to this the code insight technology, DLL debugging support, component templates, the TeeChart, the Decision Cube, the WebBroker technology, component packages, ActiveForms, and an astonishing integration with COM, thanks to interfaces. Delphi 4 gave us the AppBrowser editor, new Windows 98 features, improved OLE and COM support, extended database components, and many additions to the core VCL classes, including support for docking, constraining, and anchoring controls. Delphi 5 added to the picture many more improvements of the IDE (too many to list here), extended database support (with specific ADO and InterBase datasets), an improved version of MIDAS with Internet support, the TeamSource version-control tool, translation capabilities, the concept of frames, and new components. Delphi 6 added to all these features support for cross-platform development with the Component Library for Cross-Platform (CLX), an extended run-time library, the dbExpress database engine, Web services and exceptional XML support, a powerful Web development framework, more IDE enhancements, and a plethora of components and classes, still covered in detail in the following pages. Delphi 7 did make some of these newer technologies more robust with improvement and fixes (SOAP support and DataSnap come to mind) and offers support for newer technologies (like Windows XP themes or UDDI), but it most importantly makes readily available an interesting set of third-party tools: the RAVE reporting engine, the IntraWeb web application development technology, and the ModelMaker design environment. Finally, it opens up a brand new world by providing (even if in a preview version) the first Borland compiler for the Pascal/Delphi language not targeting the Intel CPU, but rather the .NET CIL platform. Delphi is a great tool, but it is also a complex programming environment that involves many elements. This book will help you master Delphi programming, including the Delphi language, components (both using the existing ones and developing your own), database and client/server support, the key elements of Windows and COM programming, and Internet and Web development. You do not need in-depth knowledge of any of these topics to read this book, but you do need to know the basics of programming. Having some familiarity with Delphi will help you considerably, particularly after the introductory chapters. The book starts covering its topics in depth immediately; much of the introductory material from previous editions has been removed. Some of this material and an introduction to Pascal is available on my website, as discussed in Appendix C. [...]... Language Chapter 3: The Run-Time Library Chapter 4: Core Library Classes Chapter 5: Visual Controls Chapter 6: Building the User Interface Chapter 7: Working with Forms Chapter 1: Delphi 7 and Its IDE Overview In a visual programming tool such as Delphi, the role of the integrated development environment (IDE) is at times even more important than the programming language Delphi 7 provides some interesting... programmer, don't be afraid The Delphi IDE is quite intuitive to use Delphi itself includes a manual (available in Acrobat format on the Delphi Companion Tools CD) with a tutorial that introduces the development of Delphi applications You can find a simpler introduction to Delphi and its IDE in my Essential Delphi online book (discussed in Appendix C, "Free Companion Books on Delphi" ) Throughout this book,... change considerably depending on third-party tools and wizards you've installed In Delphi 7, ModelMaker has its own menu You'll see other menus by installing popular add-ons like GExperts or even my own wizards (see Appendix B, "Extra Delphi Tools from other Sources" and A, "Extra Delphi Tools by the Author," respectively, for more details) A relevant menu added to Delphi in recent editions is the Window... TForm1.FormCreate(Sender: TObject); begin // TODO -oMarco: Add creation code end; The same item can be visually edited in the window shown in Figure 1.3, along with the To-Do List window Figure 1.3: The Edit To-Do Item window can be used to modify a to-do item, an operation you can also do directly in the source code The exception to this two-way rule is the definition of project-wide to-do items You must add these items... feature, the msginfo70.ini file of Delphi' s bin folder The Delphi Editor On the surface, Delphi' s editor doesn't appear to have changed much for version 7 of the IDE However, behind the scenes, it is a totally new tool Besides using it to work on files in the Object Pascal language (or the Delphi language, as Borland prefers to call it now), you can now use it to work on other files used in Delphi development... Diagram view is active, and Delphi prompts you for options as shown in Figure 1 .7 You can customize the output in many ways Figure 1 .7: The Print Options for the Diagram view The information in the Diagram view is saved in a separate file, not as part of the DFM file Delphi 5 used design-time information (DTI) files, which had a structure similar to INI files Delphi 6 and 7 can still read the older... an older version of Delphi might not accept a new property of a control in a DFM created by a newer version of Delphi, the older Delphis will still be able to read the rest of the text DFM file If the newer version of Delphi adds a new data type, though, older Delphis will be unable to read the newer Delphi' s binary DFMs at all Even if this doesn't sound likely, remember that 64-bit operating systems... covers the architecture of Delphi applications, the development of custom components, the use of libraries and packages, modeling with ModelMaker, and COM+ • Part III, "Delphi Database-Oriented Architectures," covers plain database access, in-depth coverage of the data-aware controls, client/server programming, dbExpress, InterBase, ADO, DataSnap, the development of custom data-aware controls and data... basic hands-on operations of the IDE; all the chapters after this one focus on programming issues and techniques Editions of Delphi Before delving into the details of the Delphi programming environment, let's take a side step to underline two key ideas First, there isn't a single edition of Delphi; there are many of them Second, any Delphi environment can be customized For these reasons, Delphi screens... introduces new features of the Delphi 7 Integrated Development Environment (IDE) in Chapter 1, then moves to the Delphi language and to the run-time library (RTL) and Visual Component Library (VCL) Four chapters in this part provide both foundations and coverage of the most commonly used controls, the development of advanced user interfaces, and the use of forms • Part II, "Delphi Object-Oriented Architectures," . Part I - Foundations Ch apt er 1 - Delphi 7 and Its IDE Ch apt er 2 - The Delphi Programming Language Ch apt er 3 - The Run-Time Library Ch apt er 4 - Core Library Classes Ch apt er 5 - Visual. Controls Ch apt er 6 - Building the User Interface Ch apt er 7 - Working with Forms Part II - Delphi Object-Oriented Architectures Ch apt er 8 - The Architecture of Delphi Applications Ch apt er 9 - Writing Delphi. Mastering Delphi 7 by Marco Cantu ISBN: 078 214201X Sybex © 2003 (1011 pages) The best Delphi resource now updated and expanded. Companion Web Site Table of Contents Mastering Delphi 7 Introduction