Delphi 6 for Windows - Developer's Guide (Borland, 2001).pdf

(14304 KB) Pobierz
Developer's Guide
Developer’s Guide
Borland ®
Delphi 6
for Windows
Borland Software Corporation
100 Enterprise Way, Scotts Valley, CA 95066-3249
287803761.001.png
Refer to the DEPLOY document located in the root directory of your Delphi 6 product for a complete list of files that
you can distribute in accordance with the Delphi 6 License Statement and Limited Warranty.
Inprise may have patents and/or pending patent applications covering subject matter in this document. The
furnishing of this document does not give you any license to these patents.
C OPYRIGHT © 1983, 2001 Borland Software Corporation. All rights reserved. All Borland brand and product names
are trademarks or registered trademarks of Borland Software Corporation. Other brand and product names are
trademarks or registered trademarks of their respective holders.
Printed in the U.S.A.
HDE1350WW21001 1E0R0501
0102030405-9 8 7654321
D3
Contents
Chapter 1
Introduction 1-1
What’s in this manual? . . . . . . . . . . . . . . 1-1
Manual conventions . . . . . . . . . . . . . . . . 1-3
Developer support services . . . . . . . . . . . . 1-3
Ordering printed documentation . . . . . . . . 1-3
TPersistent branch. . . . . . . . . . . . . . . 3-14
TComponent branch . . . . . . . . . . . . . 3-15
TControl branch . . . . . . . . . . . . . . . . 3-16
TWinControl/TWidgetControl branch . . . 3-17
Properties common to TControl . . . . . . . 3-18
Action properties . . . . . . . . . . . . . 3-18
Position, size, and alignment
properties . . . . . . . . . . . . . . . . . 3-19
Display properties . . . . . . . . . . . . . 3-19
Parent properties. . . . . . . . . . . . . . 3-19
A navigation property. . . . . . . . . . . 3-19
Drag-and-drop properties . . . . . . . . 3-20
Drag-and-dock properties
(VCL only) . . . . . . . . . . . . . . . . 3-20
Standard events common to TControl . . . 3-20
Properties common to TWinControl and
TWidgetControl . . . . . . . . . . . . . . . 3-21
General information properties . . . . . 3-21
Border style display properties. . . . . . 3-22
Navigation properties . . . . . . . . . . . 3-22
Drag-and-dock properties
(VCL only) . . . . . . . . . . . . . . . . 3-22
Events common to TWinControl and
TWidgetControl . . . . . . . . . . . . . . . 3-22
Creating the application user interface . . . 3-23
Using Delphi components . . . . . . . . . . 3-23
Setting component properties . . . . . . . . 3-24
Using the Object Inspector . . . . . . . . 3-24
Using property editors . . . . . . . . . . 3-25
Setting properties at runtime . . . . . . . 3-25
Calling methods . . . . . . . . . . . . . . . . 3-25
Working with events and event
handlers. . . . . . . . . . . . . . . . . . . . 3-25
Generating a new event handler . . . . . 3-26
Generating a handler for a component’s
default event . . . . . . . . . . . . . . . 3-26
Locating event handlers. . . . . . . . . . 3-26
Associating an event with an existing
event handler . . . . . . . . . . . . . . . 3-27
Associating menu events with event
handlers . . . . . . . . . . . . . . . . . . 3-28
Deleting event handlers . . . . . . . . . . 3-28
VCL and CLX components . . . . . . . . . . . 3-28
Adding custom components to the
Component palette . . . . . . . . . . . . . 3-30
Text controls . . . . . . . . . . . . . . . . . . 3-31
Text control properties. . . . . . . . . . . 3-31
Part I
Programming with Delphi
Chapter 2
Developing applications with Delphi 2-1
Integrated development environment . . . . . . 2-1
Designing applications . . . . . . . . . . . . . . 2-2
Developing applications . . . . . . . . . . . . . 2-3
Creating projects . . . . . . . . . . . . . . . . 2-3
Editing code. . . . . . . . . . . . . . . . . . . 2-4
Compiling applications . . . . . . . . . . . . 2-4
Debugging applications . . . . . . . . . . . . 2-5
Deploying applications . . . . . . . . . . . . 2-5
Chapter 3
Using the component libraries 3-1
Understanding the component libraries. . . . . 3-1
Properties, methods, and events . . . . . . . 3-2
Properties . . . . . . . . . . . . . . . . . . 3-2
Methods . . . . . . . . . . . . . . . . . . . 3-3
Events . . . . . . . . . . . . . . . . . . . . 3-3
User events . . . . . . . . . . . . . . . . . 3-3
System events . . . . . . . . . . . . . . . . 3-4
Object Pascal and the class libraries . . . . . . . 3-4
Using the object model . . . . . . . . . . . . 3-4
What is an object? . . . . . . . . . . . . . . 3-5
Examining a Delphi object . . . . . . . . . 3-5
Changing the name of a component . . . 3-7
Inheriting data and code from an object . . . 3-8
Scope and qualifiers . . . . . . . . . . . . . . 3-8
Private, protected, public, and published
declarations . . . . . . . . . . . . . . . . 3-9
Using object variables . . . . . . . . . . . . . 3-10
Creating, instantiating, and destroying
objects . . . . . . . . . . . . . . . . . . . . . 3-11
Components and ownership . . . . . . . . . 3-11
Objects, components, and controls . . . . . . . . 3-12
TObject branch . . . . . . . . . . . . . . . . . 3-14
iii
287803761.002.png
Properties of memo and rich text
controls . . . . . . . . . . . . . . . . . . . 3-31
Rich text controls (VCL only) . . . . . . . 3-32
Specialized input controls . . . . . . . . . . . 3-32
Scroll bars . . . . . . . . . . . . . . . . . . 3-32
Track bars . . . . . . . . . . . . . . . . . . 3-33
Up-down controls (VCL only). . . . . . . 3-33
Spin edit controls (CLX only) . . . . . . . 3-33
Hot key controls (VCL only). . . . . . . . 3-33
Splitter controls . . . . . . . . . . . . . . . 3-34
Buttons and similar controls . . . . . . . . . 3-34
Button controls . . . . . . . . . . . . . . . 3-34
Bitmap buttons . . . . . . . . . . . . . . . 3-35
Speed buttons . . . . . . . . . . . . . . . . 3-35
Check boxes . . . . . . . . . . . . . . . . . 3-35
Radio buttons . . . . . . . . . . . . . . . . 3-35
Toolbars . . . . . . . . . . . . . . . . . . . 3-36
Cool bars (VCL only) . . . . . . . . . . . . 3-36
Handling lists . . . . . . . . . . . . . . . . . . 3-36
List boxes and check-list boxes . . . . . . 3-37
Combo boxes . . . . . . . . . . . . . . . . 3-37
Tree views . . . . . . . . . . . . . . . . . . 3-38
List views . . . . . . . . . . . . . . . . . . 3-38
Date-time pickers and month
calendars (VCL only) . . . . . . . . . . . 3-39
Grouping components . . . . . . . . . . . . . 3-39
Group boxes and radio groups . . . . . . 3-39
Panels . . . . . . . . . . . . . . . . . . . . 3-39
Scroll boxes . . . . . . . . . . . . . . . . . 3-40
Tab controls . . . . . . . . . . . . . . . . . 3-40
Page controls . . . . . . . . . . . . . . . . 3-40
Header controls . . . . . . . . . . . . . . . 3-41
Providing visual feedback . . . . . . . . . . . 3-41
Labels and static text components . . . . 3-41
Status bars . . . . . . . . . . . . . . . . . . 3-42
Progress bars . . . . . . . . . . . . . . . . 3-42
Help and hint properties . . . . . . . . . . 3-42
Grids . . . . . . . . . . . . . . . . . . . . . . . 3-43
Draw grids. . . . . . . . . . . . . . . . . . 3-43
String grids . . . . . . . . . . . . . . . . . 3-43
Value list editors (VCL only) . . . . . . . . . 3-43
Displaying graphics . . . . . . . . . . . . . . 3-44
Images . . . . . . . . . . . . . . . . . . . . 3-44
Shapes . . . . . . . . . . . . . . . . . . . . 3-44
Bevels . . . . . . . . . . . . . . . . . . . . 3-45
Paint boxes. . . . . . . . . . . . . . . . . . 3-45
Animation control (VCL only). . . . . . . 3-45
Developing dialog boxes . . . . . . . . . . . 3-45
Using open dialog boxes . . . . . . . . . . 3-46
Using helper objects . . . . . . . . . . . . . . . 3-46
Working with lists . . . . . . . . . . . . . . . 3-47
Working with string lists . . . . . . . . . . . 3-47
Loading and saving string lists. . . . . . 3-48
Creating a new string list . . . . . . . . . 3-48
Manipulating strings in a list . . . . . . . 3-50
Associating objects with a string list . . . 3-52
Windows registry and INI files . . . . . . . 3-52
Using TIniFile (VCL only) . . . . . . . . 3-52
Using TRegistry . . . . . . . . . . . . . . 3-53
Using TRegIniFile . . . . . . . . . . . . . 3-53
Creating drawing spaces . . . . . . . . . . . 3-54
Printing . . . . . . . . . . . . . . . . . . . . . 3-54
Using streams . . . . . . . . . . . . . . . . . 3-55
Chapter 4
Common programming tasks 4-1
Understanding classes . . . . . . . . . . . . . . . 4-1
Defining classes . . . . . . . . . . . . . . . . . . . 4-2
Handling exceptions . . . . . . . . . . . . . . . . 4-4
Protecting blocks of code . . . . . . . . . . . . 4-4
Responding to exceptions. . . . . . . . . . 4-5
Exceptions and the flow of control. . . . . 4-6
Nesting exception responses . . . . . . . . 4-6
Protecting resource allocations. . . . . . . . . 4-7
What kind of resources need
protection? . . . . . . . . . . . . . . . . . 4-7
Creating a resource protection block. . . . 4-8
Handling RTL exceptions. . . . . . . . . . . . 4-9
What are RTL exceptions? . . . . . . . . . 4-9
Creating an exception handler . . . . . . 4-10
Exception handling statements. . . . . . 4-11
Using the exception instance . . . . . . . 4-11
Scope of exception handlers . . . . . . . 4-12
Providing default exception
handlers . . . . . . . . . . . . . . . . . . 4-12
Handling classes of exceptions . . . . . . 4-13
Reraising the exception . . . . . . . . . . 4-13
Handling component exceptions . . . . . . 4-14
Exception handling with external
sources . . . . . . . . . . . . . . . . . . . . 4-15
Silent exceptions . . . . . . . . . . . . . . . . 4-15
Defining your own exceptions . . . . . . . . 4-16
Declaring an exception object type. . . . 4-16
Raising an exception. . . . . . . . . . . . 4-17
Using interfaces . . . . . . . . . . . . . . . . . . 4-17
Interfaces as a language feature . . . . . . . 4-18
Implementing interfaces across
the hierarchy . . . . . . . . . . . . . . . 4-18
iv
Using interfaces with procedures . . . . . 4-20
Implementing IInterface . . . . . . . . . . . . 4-20
TInterfacedObject . . . . . . . . . . . . . . . 4-21
Using the as operator . . . . . . . . . . . . . 4-21
Reusing code and delegation . . . . . . . . . 4-22
Using implements for delegation . . . . . 4-22
Aggregation . . . . . . . . . . . . . . . . . 4-23
Memory management of interface
objects . . . . . . . . . . . . . . . . . . . . . 4-24
Using reference counting . . . . . . . . . 4-24
Not using reference counting . . . . . . . 4-25
Using interfaces in distributed
applications (VCL only) . . . . . . . . . . . 4-26
Defining custom variants . . . . . . . . . . . . . 4-27
Storing a custom variant type’s data . . . . . 4-28
Creating a class to enable the custom
variant type . . . . . . . . . . . . . . . . . . 4-28
Enabling casting . . . . . . . . . . . . . . 4-29
Implementing binary operations . . . . . 4-30
Implementing comparison operations . . 4-32
Implementing unary operations . . . . . 4-34
Copying and clearing custom
variants. . . . . . . . . . . . . . . . . . . 4-34
Loading and saving custom variant
values. . . . . . . . . . . . . . . . . . . . 4-35
Using the TCustomVariantType
descendant . . . . . . . . . . . . . . . . . 4-36
Writing utilities to work with a custom
variant type . . . . . . . . . . . . . . . . . . 4-36
Supporting properties and methods in
custom variants . . . . . . . . . . . . . . . . 4-37
Using TInvokeableVariantType . . . . . . 4-37
Using TPublishableVariantType. . . . . . 4-39
Working with strings . . . . . . . . . . . . . . . 4-39
Character types . . . . . . . . . . . . . . . . . 4-39
String types . . . . . . . . . . . . . . . . . . . 4-40
Short strings . . . . . . . . . . . . . . . . . 4-40
Long strings . . . . . . . . . . . . . . . . . 4-41
WideString. . . . . . . . . . . . . . . . . . 4-41
PChar types . . . . . . . . . . . . . . . . . 4-42
OpenString . . . . . . . . . . . . . . . . . 4-42
Runtime library string handling routines . . 4-42
Wide character routines . . . . . . . . . . 4-43
Commonly used long string routines. . . 4-43
Declaring and initializing strings . . . . . . . 4-46
Mixing and converting string types . . . . . 4-47
String to PChar conversions. . . . . . . . . . 4-47
String dependencies . . . . . . . . . . . . 4-47
Returning a PChar local variable . . . . . 4-48
Passing a local variable as a PChar . . . 4-48
Compiler directives for strings. . . . . . . . 4-49
Strings and characters: related topics . . . . 4-50
Working with files . . . . . . . . . . . . . . . . 4-50
Manipulating files . . . . . . . . . . . . . . . 4-50
Deleting a file. . . . . . . . . . . . . . . . 4-50
Finding a file . . . . . . . . . . . . . . . . 4-51
Renaming a file. . . . . . . . . . . . . . . 4-52
File date-time routines . . . . . . . . . . 4-52
Copying a file . . . . . . . . . . . . . . . 4-53
File types with file I/O . . . . . . . . . . . . 4-53
Using file streams . . . . . . . . . . . . . . . 4-54
Creating and opening files . . . . . . . . 4-54
Using the file handle . . . . . . . . . . . 4-55
Reading and writing to files . . . . . . . 4-55
Reading and writing strings . . . . . . . 4-56
Seeking a file . . . . . . . . . . . . . . . . 4-56
File position and size . . . . . . . . . . . 4-57
Copying . . . . . . . . . . . . . . . . . . . 4-57
Converting measurements . . . . . . . . . . . . 4-58
Performing conversions . . . . . . . . . . . 4-58
Performing simple conversions . . . . . 4-58
Performing complex conversions . . . . 4-58
Adding new measurement types . . . . . . 4-59
Creating a simple conversion family
and adding units . . . . . . . . . . . . . 4-59
Using a conversion function . . . . . . . 4-60
Using a class to manage
conversions . . . . . . . . . . . . . . . . 4-62
Defining data types . . . . . . . . . . . . . . . . 4-64
Chapter 5
Building applications, components,
and libraries 5-1
Creating applications . . . . . . . . . . . . . . . . 5-1
GUI applications. . . . . . . . . . . . . . . . . 5-1
User interface models . . . . . . . . . . . . 5-2
SDI applications . . . . . . . . . . . . . . . 5-2
MDI applications. . . . . . . . . . . . . . . 5-2
Setting IDE, project, and compilation
options . . . . . . . . . . . . . . . . . . . 5-3
Programming templates . . . . . . . . . . . . 5-3
Console applications . . . . . . . . . . . . . . 5-3
Service applications . . . . . . . . . . . . . . . 5-4
Service threads . . . . . . . . . . . . . . . . 5-6
Service name properties. . . . . . . . . . . 5-7
Debugging services . . . . . . . . . . . . . 5-8
Creating packages and DLLs . . . . . . . . . . . 5-9
When to use packages and DLLs . . . . . . . 5-9
v
Zgłoś jeśli naruszono regulamin