Borland Delphi 7 Developer's Guide.pdf

(14746 KB) Pobierz
Developer's Guide, Delphi 7 for Windows
Developer’s Guide
Borland ®
Delphi 7
for Windows
Borland Software Corporation
100 Enterprise Way, Scotts Valley, CA 95066-3249
www.borland.com
126196733.001.png
Refer to the DEPLOY document located in the root directory of your Delphi 7 product for a complete list of files that
you can distribute in accordance with the Delphi 7 License Statement and Limited Warranty.
Borland Software Corporation may have patents and/or pending patent applications covering subject matter in this
document. Please refer to the product CD or the About dialog box for the list of applicable patents. The furnishing of
this document does not give you any license to these patents.
C OPYRIGHT © 1983–2002 Borland Software Corporation. All rights reserved. All Borland brand and product names
are trademarks or registered trademarks of Borland Software Corporation in the United States and other countries.
All other marks are the property of their respective owners.
Printed in the U.S.A.
HDE1370WW21001 7E5R0802
0203040506-9 8 7654321
D3
Contents
Chapter 1
Introduction 1-1
What’s in this manual? . . . . . . . . . . . . . . 1-1
Manual conventions . . . . . . . . . . . . . . . . 1-2
Developer support services . . . . . . . . . . . . 1-3
Using object variables . . . . . . . . . . . . . . 4-7
Creating, instantiating, and destroying
objects . . . . . . . . . . . . . . . . . . . . . . 4-8
Components and ownership . . . . . . . . . 4-9
Defining new classes . . . . . . . . . . . . . . . 4-9
Using interfaces . . . . . . . . . . . . . . . . . . 4-12
Using interfaces across the hierarchy . . . . 4-13
Using interfaces with procedures . . . . . . 4-14
Implementing IInterface . . . . . . . . . . . 4-14
TInterfacedObject . . . . . . . . . . . . . . . 4-15
Using the as operator with interfaces . . . . 4-16
Reusing code and delegation. . . . . . . . . 4-16
Using implements for delegation . . . . 4-17
Aggregation . . . . . . . . . . . . . . . . 4-18
Memory management of interface
objects . . . . . . . . . . . . . . . . . . . . . 4-18
Using reference counting . . . . . . . . 4-19
Not using reference counting . . . . . . 4-20
Using interfaces in distributed
applications . . . . . . . . . . . . . . . . . 4-21
Part I
Programming with Delphi
Chapter 2
Developing applications with Delphi 2-1
Integrated development environment . . . . . . 2-1
Designing applications . . . . . . . . . . . . . . 2-2
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 library 3-1
Understanding the component library . . . . . 3-1
Properties, methods, and events . . . . . . . 3-3
Properties . . . . . . . . . . . . . . . . . . 3-3
Methods . . . . . . . . . . . . . . . . . . 3-4
Events . . . . . . . . . . . . . . . . . . . . 3-4
User events . . . . . . . . . . . . . . . . . 3-4
System events . . . . . . . . . . . . . . . 3-4
Internal events . . . . . . . . . . . . . . . 3-4
Objects, components, and controls . . . . . . . . 3-5
TObject branch . . . . . . . . . . . . . . . . . 3-6
TPersistent branch . . . . . . . . . . . . . . . 3-7
TComponent branch . . . . . . . . . . . . . . 3-7
TControl branch . . . . . . . . . . . . . . . . 3-9
TWinControl/TWidgetControl branch . . . 3-10
Chapter 5
Using BaseCLX 5-1
Using streams . . . . . . . . . . . . . . . . . . . 5-2
Using streams to read or write data . . . . . 5-2
Stream methods for reading
and writing . . . . . . . . . . . . . . . 5-2
Reading and writing components . . . 5-3
Reading and writing strings . . . . . . . 5-3
Copying data from one stream
to another. . . . . . . . . . . . . . . . . . . 5-4
Specifying the stream position and size. . . 5-4
Seeking to a specific position . . . . . . 5-4
Using Position and Size properties . . . 5-5
Working with files . . . . . . . . . . . . . . . . 5-5
Approaches to file I/O . . . . . . . . . . . . 5-6
Using file streams . . . . . . . . . . . . . . . 5-6
Creating and opening files using
file streams . . . . . . . . . . . . . . . 5-7
Using the file handle . . . . . . . . . . . 5-8
Manipulating files . . . . . . . . . . . . . . . 5-8
Deleting a file . . . . . . . . . . . . . . . 5-8
Finding a file . . . . . . . . . . . . . . . 5-8
Renaming a file . . . . . . . . . . . . . . 5-10
File date-time routines . . . . . . . . . . 5-10
Copying a file . . . . . . . . . . . . . . . 5-11
Chapter 4
Using the object model 4-1
What is an object? . . . . . . . . . . . . . . . . . 4-1
Examining a Delphi object . . . . . . . . . . 4-2
Changing the name of a component . . . . . 4-4
Inheriting data and code from an object. . . . . 4-5
Scope and qualifiers . . . . . . . . . . . . . . . . 4-5
Private, protected, public, and published
declarations . . . . . . . . . . . . . . . . . . 4-6
iii
126196733.002.png
Working with ini files and the system
Registry . . . . . . . . . . . . . . . . . . . . . 5-11
Using TIniFile and TMemIniFile . . . . 5-12
Using TRegistryIniFile . . . . . . . . . 5-13
Using TRegistry . . . . . . . . . . . . . 5-13
Working with lists . . . . . . . . . . . . . . . . 5-14
Common list operations . . . . . . . . . . . 5-15
Adding list items . . . . . . . . . . . . 5-15
Deleting list items . . . . . . . . . . . . 5-15
Accessing list items . . . . . . . . . . . 5-16
Rearranging list items . . . . . . . . . . 5-16
Persistent lists. . . . . . . . . . . . . . . . . 5-16
Working with string lists . . . . . . . . . . . . 5-17
Loading and saving string lists . . . . . . . 5-17
Creating a new string list . . . . . . . . . . 5-18
Short-term string lists . . . . . . . . . . 5-18
Long-term string lists . . . . . . . . . . 5-18
Manipulating strings in a list . . . . . . . . 5-20
Counting the strings in a list . . . . . . 5-20
Accessing a particular string . . . . . . 5-20
Locating items in a string list . . . . . . 5-20
Iterating through strings in a list . . . . 5-20
Adding a string to a list . . . . . . . . . 5-21
Moving a string within a list . . . . . . 5-21
Deleting a string from a list . . . . . . . 5-21
Associating objects with a
string list . . . . . . . . . . . . . . . . 5-22
Working with strings . . . . . . . . . . . . . . 5-22
Wide character routines . . . . . . . . . . . 5-22
Commonly used long string routines . . . 5-23
Commonly used routines for
null-terminated strings. . . . . . . . . . . 5-26
Declaring and initializing strings . . . . . . 5-27
Mixing and converting string types . . . . 5-28
String to PChar conversions. . . . . . . . . 5-28
String dependencies . . . . . . . . . . . 5-29
Returning a PChar local variable . . . 5-29
Passing a local variable as
a PChar . . . . . . . . . . . . . . . . . 5-29
Compiler directives for strings . . . . . . . 5-30
Creating drawing spaces . . . . . . . . . . . . 5-31
Printing . . . . . . . . . . . . . . . . . . . . . . 5-32
Converting measurements . . . . . . . . . . . 5-33
Performing conversions . . . . . . . . . . . 5-33
Performing simple conversions . . . . 5-33
Performing complex conversions . . . 5-33
Adding new measurement types . . . . . . 5-34
Creating a simple conversion family
and adding units. . . . . . . . . . . . . . . 5-34
Declare variables . . . . . . . . . . . . . 5-35
Register the conversion family . . . . . 5-35
Register measurement units . . . . . . . 5-35
Use the new units . . . . . . . . . . . . . 5-35
Using a conversion function . . . . . . . . . 5-36
Declare variables . . . . . . . . . . . . . 5-36
Register the conversion family . . . . . 5-36
Register the base unit . . . . . . . . . . 5-36
Write methods to convert to and
from the base unit . . . . . . . . . . . . 5-36
Register the other units . . . . . . . . . 5-37
Use the new units . . . . . . . . . . . . . 5-37
Using a class to manage conversions . . . . 5-37
Creating the conversion class . . . . . . 5-38
Declare variables . . . . . . . . . . . . . 5-39
Register the conversion family and
the other units . . . . . . . . . . . . . . 5-39
Use the new units . . . . . . . . . . . . . 5-40
Defining custom variants . . . . . . . . . . . . 5-40
Storing a custom variant type’s data . . . . 5-41
Creating a class to enable the
custom variant type . . . . . . . . . . . . . 5-42
Enabling casting . . . . . . . . . . . . . 5-42
Implementing binary operations . . . . 5-44
Implementing comparison
operations . . . . . . . . . . . . . . . . 5-46
Implementing unary operations . . . . 5-47
Copying and clearing custom variants . . . 5-48
Loading and saving custom
variant values . . . . . . . . . . . . . . 5-49
Using the TCustomVariantType
descendant . . . . . . . . . . . . . . . . 5-50
Writing utilities to work with a
custom variant type . . . . . . . . . . . . . 5-50
Supporting properties and methods
in custom variants . . . . . . . . . . . . . . 5-51
Using TInvokeableVariantType . . . . . 5-51
Using TPublishableVariantType . . . . 5-53
Chapter 6
Working with components 6-1
Setting component properties . . . . . . . . . . 6-2
Setting properties at design time . . . . . . 6-2
Using property editors . . . . . . . . . . 6-3
Setting properties at runtime. . . . . . . . . 6-3
Calling methods. . . . . . . . . . . . . . . . . . 6-3
iv
Working with events and event handlers . . . . 6-3
Generating a new event handler . . . . . . . 6-4
Generating a handler for a
component’s default event . . . . . . . . . 6-4
Locating event handlers . . . . . . . . . . . . 6-4
Associating an event with an existing
event handler . . . . . . . . . . . . . . . . . 6-5
Using the Sender parameter . . . . . . . 6-5
Displaying and coding shared
events . . . . . . . . . . . . . . . . . . . 6-5
Associating menu events with
event handlers . . . . . . . . . . . . . . . . 6-6
Deleting event handlers . . . . . . . . . . . . 6-6
Cross-platform and non-cross-platform
components . . . . . . . . . . . . . . . . . . . . 6-7
Adding custom components to the
Component palette . . . . . . . . . . . . . . 6-9
Adding graphics to controls . . . . . . . . . . . 7-13
Indicating that a control is
owner-drawn. . . . . . . . . . . . . . . . . 7-13
Adding graphical objects to
a string list . . . . . . . . . . . . . . . . . . 7-14
Adding images to an application . . . . 7-14
Adding images to a string list . . . . . . 7-14
Drawing owner-drawn items . . . . . . 7-15
Sizing owner-draw items . . . . . . . . . . . 7-16
Drawing owner-draw items . . . . . . . . . 7-17
Chapter 8
Building applications, components,
and libraries 8-1
Creating applications . . . . . . . . . . . . . . . 8-1
GUI applications. . . . . . . . . . . . . . . . 8-2
User interface models . . . . . . . . . . 8-2
SDI applications . . . . . . . . . . . . . 8-2
MDI applications . . . . . . . . . . . . . 8-2
Setting IDE, project, and compiler
options . . . . . . . . . . . . . . . . . . 8-3
Programming templates . . . . . . . . . . . 8-3
Console applications . . . . . . . . . . . . . 8-4
Service applications . . . . . . . . . . . . . . 8-5
Service threads . . . . . . . . . . . . . . 8-8
Service name properties . . . . . . . . . 8-9
Debugging service applications . . . . . 8-10
Creating packages and DLLs . . . . . . . . . . 8-11
When to use packages and DLLs . . . . . . 8-11
Writing database applications . . . . . . . . . . 8-12
Distributing database applications . . . . . 8-13
Creating Web server applications . . . . . . . . 8-13
Creating Web Broker applications . . . . . . 8-14
Creating WebSnap applications . . . . . . . 8-15
Creating Web Services applications . . . . . 8-15
Writing applications using COM . . . . . . . . 8-16
Using COM and DCOM . . . . . . . . . . . 8-16
Using MTS and COM+ . . . . . . . . . . . . 8-16
Using data modules . . . . . . . . . . . . . . . 8-17
Creating and editing standard data
modules. . . . . . . . . . . . . . . . . . . . 8-17
Naming a data module and
its unit file . . . . . . . . . . . . . . . . 8-18
Placing and naming components . . . . 8-19
Using component properties and
events in a data module . . . . . . . . 8-19
Creating business rules in a
data module . . . . . . . . . . . . . . . 8-20
Chapter 7
Working with controls 7-1
Implementing drag and drop in controls . . . . 7-1
Starting a drag operation . . . . . . . . . . . 7-1
Accepting dragged items . . . . . . . . . . . 7-2
Dropping items . . . . . . . . . . . . . . . . . 7-3
Ending a drag operation . . . . . . . . . . . . 7-3
Customizing drag and drop with
a drag object . . . . . . . . . . . . . . . . . . 7-3
Changing the drag mouse pointer . . . . . . 7-4
Implementing drag and dock in controls . . . . 7-4
Making a windowed control a
docking site . . . . . . . . . . . . . . . . . . 7-4
Making a control a dockable child . . . . . . 7-5
Controlling how child controls
are docked . . . . . . . . . . . . . . . . . . . 7-5
Controlling how child controls
are undocked . . . . . . . . . . . . . . . . . 7-6
Controlling how child controls respond
to drag-and-dock operations . . . . . . . . 7-6
Working with text in controls. . . . . . . . . . . 7-6
Setting text alignment . . . . . . . . . . . . . 7-7
Adding scroll bars at runtime . . . . . . . . . 7-7
Adding the clipboard object. . . . . . . . . . 7-8
Selecting text . . . . . . . . . . . . . . . . . . 7-9
Selecting all text . . . . . . . . . . . . . . . . 7-9
Cutting, copying, and pasting text . . . . . 7-10
Deleting selected text . . . . . . . . . . . . 7-10
Disabling menu items . . . . . . . . . . . . 7-11
Providing a pop-up menu . . . . . . . . . . 7-11
Handling the OnPopup event. . . . . . . . 7-12
v
Zgłoś jeśli naruszono regulamin