C++Builder 6 Developer's Guide
Total Page:16
File Type:pdf, Size:1020Kb
C++Builder™ 6 Developer’s Guide Satya Sai Kolachina Wordware Publishing, Inc. Library of Congress Cataloging-in-Publication Data Sai Kolachina, Satya. C++builder 6 developer’s guide / by Satya Sai Kolachina. p. cm. Includes bibliographical references and index. ISBN 1-55622-960-7 (paperback) 1. Computer software—Development. 2. Delphi (Computer file). 3. Telecommunication systems. I. Title. QA76.76.D47 M665 2002 005.1--dc21 2002011 CIP © 2003, Wordware Publishing, Inc. All Rights Reserved 2320 Los Rios Boulevard Plano, Texas 75074 No part of this book may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc. Printed in the United States of America ISBN 1-55622-960-7 10987654321 0211 C++Builder is a trademark of Borland Software Corporation in the United States and other countries. Other products mentioned are used for identification purposes only and may be trademarks of their respective companies. All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address. Telephone inquiries may be made by calling: (972) 423-0090 Dedication and Acknowledgments It is my pleasure and responsibility to dedicate this book to a few people without who I am incomplete in several aspects. First of all, I mention my father, Sri Venkata Lakshmana Murthy Kolachina, and mother, the late Srimathi Kamala Devi Kolachina, who gave me this invaluable life, taught me human values, and with whose blessings, efforts, and encouragement I am now what they envisioned I should be. Next I mention Sri Vinai Bhushan VR Kanteti and his wife, Srimathi Swarajya Lakshmi Kanteti, who gave me a career and laid the foundation stone for my social status today. There is a saying “Behind every successful man is a woman.” It is also said that without a wife’s cooperation, the husband is only an incomplete person. These words are very much true in my case, since my wife, Parvathi Devi Kolachina, has provided me encouragement and full cooper- ation at every step in my life, career, and even while writing this book. I express my gratitude to her whole-heartedly. I am very proud of my children, Kamala Priyadarsini Kolachina and Satya Shilpa Kolachina, who always regarded their dad as a great person and always extended their love, affection, and cooperation, though I am a very normal person like millions of others in this world. I next thank very much the reviewers, Damon Chandler and Thomas J. Theobald, who gave me their valuable suggestions to better present my thoughts. I also thank my close friends and relatives who provided me encouragement. I thank Wordware Publishing and its staff for extending me their full cooperation throughout the process. I am very thankful to Karen Giles at Borland Developer Relations, who provided me with the C++Builder 6 software and encouraged me to write this book. I am a true believer that every human being is a representation of God. Therefore, I thank the creator of this universe who provided me so many well-wishers and a continuous flow of thoughts to keep my commitment to the publisher and to the reader community. iii Contents Introduction ...........................xiii Part I Windows and Database Development Chapter 1 Introduction to C++Builder 6 .............3 Introduction ............................3 C++Builder 6 Overview ......................3 The Main Window and Toolbars ................3 Component Palette .......................4 Object Inspector.........................5 Object TreeView ........................7 To-Do List ..........................7 Alignment Palette ........................8 New Features ...........................9 Technology Additions......................9 WebSnap and Enhancements to Internet Development . 9 BizSnap...........................11 Enhancements .........................11 Enhancement to DataSnap .................11 New Database Support ...................11 STLPort Standard Template Library ..............11 New Components and Wizards ................12 Working with Projects ......................13 What Constitutes a C++Builder 6 Project?...........13 Setting Project Options ....................14 Borland Packages and Components ..............15 Installing Third-Party Vendor Components ..........16 Types of Projects That C++Builder 6 Supports ........17 Building User Interfaces ....................18 Your First Windows Application................19 Summary .............................22 Chapter 2 C++ Advanced Concepts ...............23 Introduction ...........................23 Language Overview .......................24 v Contents C++ Features That are Often Ignored ..............26 Templates ...........................26 What are Templates? ....................26 When to Use Templates...................26 Namespaces ..........................29 Casting in C++.........................32 dynamic_cast ........................32 static_cast..........................34 const_cast..........................34 reinterpret_cast .......................35 Exception Handling ......................35 A Word about Pointers.....................37 References ...........................40 Virtual Methods ........................42 C++ Language and C++Builder .................45 Overview ...........................45 Keyword Extensions ......................47 __classid ..........................47 __closure ..........................47 __property .........................49 __published.........................50 __declspec .........................50 __interface .........................52 Object Construction Sequence in C++Builder .........53 Summary .............................56 Chapter 3 The VCL Explored ...................57 Brief Introduction to VCL Architecture .............57 VCL Objects ...........................60 AnsiString ...........................62 What is TObject? .......................63 Persistence of Objects—TPersistent Class ...........65 Basic VCL Component—TComponent Class .........65 Non-Visual Components....................66 Visual Components—TControl and TWinControl Classes and Their Descendants ...............66 The Application—Global TApplication Instance .......67 Properties, Methods, and Events of TApplication .....69 Other Global Variables—TScreen, TMouse, and TClipboard Instances ....................73 Getting the RTTI........................79 Is a Specific Property Published for the Object? ......79 What is the Property Type? .................79 Is a Specific Property in an Object of a Specific Type? . 80 vi Contents What is the Type of a Specific Property in an Object? . 80 Is the Property Stored in the DFM File? ..........80 Getting and Setting Property Values for an Object .....80 A Closer Look at Forms .....................82 Creating Forms in a DLL ...................84 What are a Form’s Characteristics? ..............90 Frames in the VCL ........................92 Using Frames in a Project ...................93 The Value List Editor ......................95 Summary .............................97 Chapter 4 Advanced VCL Features ................99 Introduction ...........................99 Actions and ActionLists .....................99 Action Clients ........................100 Action Objects and TAction Class ..............101 Types of Action Objects ...................101 Action Targets ........................101 ActionList Object.......................102 Using the Standard Action Objects ..............102 How Does the Application Find the Action Target for an Action Object? ...................105 Using the Custom Action Objects ..............105 How Does VCL Identify the Appropriate Action Handler for an Action? ..................107 Visual Components That Render Action Objects .......110 Windows Common Dialogs...................118 Windows Drag and Drop ....................120 Properties and Methods of the Source Object.........120 BeginDrag() Method ...................120 OnMouseDown() Event ..................121 Event Handlers for the Drop Target..............121 Custom Drag Objects.....................122 Drag and Drop Across Modules in an Application ....128 Date/Time Management ....................133 TDateTime Class .......................133 What is DOS File Timestamp? ................137 Win32 Core Date-Time Management ............138 Formatting Date-Time Strings ................139 Constants Defined in SYSUTILS...............140 Directory and File Management ................141 Working with Directories...................141 Working with Files ......................144 Functions That Work with File Existence and Search . 144 vii Contents WIN32_FIND_DATA Structure..............145 File Attributes .......................146 Setting the Advanced Attributes..............147 Filename Manipulation Functions .............149 File Content Management .................151 Summary ............................152 Chapter 5 Simple Database Development ...........153 Introduction to Database Development.............153 Two-tier, Three-tier, and Multi-tier Applications .......154 VCL Architecture for Database Development .........156 Database Connection Components ..............157 Database Connection Frameworks Supported in VCL . 157 How Do You Connect to a Database? ...........160 What is TDataSet? ......................161 Dataset States .......................163 Synchronous and Asynchronous Modes..........164 Properties, Methods, and Events .............165 Data-aware Controls and DataSource.............169 Data Module—The Non-visual Container ..........170