Delphi 7 - Component Writer's Guide.pdf

(2189 KB) Pobierz
Delphi 7 for Windows Component Writer's Guide
Component Writer’s Guide
Borland ®
Delphi 7
for Windows ®
Borland Software Corporation
100 Enterprise Way, Scotts Valley, CA 95066-3249
www.borland.com
287689422.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.
D7–CWG–0802
Contents
Chapter 1
Overview of component creation 1-1
Class library . . . . . . . . . . . . . . . . . . . . 1-1
Components and classes . . . . . . . . . . . . . 1-2
Creating components . . . . . . . . . . . . . . . 1-3
Modifying existing controls . . . . . . . . . 1-3
Creating windowed controls . . . . . . . . . 1-4
Creating graphic controls . . . . . . . . . . 1-4
Subclassing Windows controls . . . . . . . 1-5
Creating nonvisual components . . . . . . . 1-5
What goes into a component? . . . . . . . . . . 1-5
Removing dependencies . . . . . . . . . . . 1-5
Setting properties, methods,
and events . . . . . . . . . . . . . . . . . . 1-6
Properties . . . . . . . . . . . . . . . . . . 1-6
Methods . . . . . . . . . . . . . . . . . . 1-7
Events . . . . . . . . . . . . . . . . . . . . 1-7
Encapsulating graphics . . . . . . . . . . . . 1-7
Registering components . . . . . . . . . . . 1-8
Creating a new component . . . . . . . . . . . 1-8
Creating a component with the
Component wizard . . . . . . . . . . . . . 1-9
Creating a component manually . . . . . 1-11
Creating a unit file . . . . . . . . . . . . 1-11
Deriving the component . . . . . . . . 1-12
Registering the component . . . . . . 1-13
Creating a bitmap for a component . . . . 1-13
Installing a component on the
Component palette . . . . . . . . . . . . . . 1-15
Making source files available . . . . . . . 1-16
Testing uninstalled components . . . . . . . 1-16
Testing installed components . . . . . . . . . 1-18
Controlling access . . . . . . . . . . . . . . . . 2-4
Hiding implementation details . . . . . . . 2-5
Defining the component
writer’s interface . . . . . . . . . . . . . . 2-6
Defining the runtime interface . . . . . . . 2-6
Defining the design-time interface . . . . . 2-7
Dispatching methods . . . . . . . . . . . . . . 2-7
Static methods . . . . . . . . . . . . . . . . 2-7
An example of static methods . . . . . . 2-8
Virtual methods . . . . . . . . . . . . . . . 2-8
Overriding methods . . . . . . . . . . . 2-8
Dynamic methods . . . . . . . . . . . . . . 2-9
Abstract class members . . . . . . . . . . . . 2-10
Classes and pointers . . . . . . . . . . . . . . 2-10
Chapter 3
Creating properties 3-1
Why create properties? . . . . . . . . . . . . . 3-1
Types of properties . . . . . . . . . . . . . . . 3-2
Publishing inherited properties . . . . . . . . 3-3
Defining properties . . . . . . . . . . . . . . . 3-3
Property declarations . . . . . . . . . . . . 3-4
Internal data storage . . . . . . . . . . . . . 3-4
Direct access . . . . . . . . . . . . . . . . . 3-5
Access methods . . . . . . . . . . . . . . . 3-5
The read method . . . . . . . . . . . . . 3-6
The write method . . . . . . . . . . . . . 3-7
Default property values . . . . . . . . . . . 3-7
Specifying no default value . . . . . . . 3-8
Creating array properties . . . . . . . . . . . . 3-8
Creating properties for subcomponents . . . . 3-9
Creating properties for interfaces . . . . . . . 3-11
Storing and loading properties . . . . . . . . . 3-11
Using the store-and-load
mechanism . . . . . . . . . . . . . . . . 3-12
Specifying default values . . . . . . . . . 3-12
Determining what to store . . . . . . . . 3-13
Initializing after loading . . . . . . . . . . 3-14
Storing and loading unpublished
properties . . . . . . . . . . . . . . . . . 3-14
Creating methods to store and
load property values . . . . . . . . . 3-15
Overriding the DefineProperties
method . . . . . . . . . . . . . . . . . 3-15
Chapter 2
Object-oriented programming for
component writers 2-1
Defining new classes . . . . . . . . . . . . . . . 2-2
Deriving new classes . . . . . . . . . . . . . 2-2
To change class defaults to
avoid repetition . . . . . . . . . . . . . 2-2
To add new capabilities to a class . . . . 2-3
Declaring a new component class . . . . . . 2-3
Ancestors, descendants, and
class hierarchies . . . . . . . . . . . . . . . . . 2-3
iii
287689422.002.png
Chapter 4
Creating events 4-1
What are events? . . . . . . . . . . . . . . . . . 4-1
Events are method pointers . . . . . . . . . 4-2
Events are properties . . . . . . . . . . . . . 4-3
Event types are method-pointer types . . . 4-3
Event-handler types are
procedures . . . . . . . . . . . . . . . . 4-3
Event handlers are optional . . . . . . . . . 4-4
Implementing the standard events . . . . . . . 4-5
Identifying standard events . . . . . . . . . 4-5
Standard events for all controls . . . . . 4-5
Standard events for
standard controls . . . . . . . . . . . . 4-5
Making events visible . . . . . . . . . . . . 4-6
Changing the standard event
handling . . . . . . . . . . . . . . . . . . . 4-6
Defining your own events . . . . . . . . . . . . 4-7
Triggering the event . . . . . . . . . . . . . 4-7
Two kinds of events . . . . . . . . . . . . 4-7
Defining the handler type . . . . . . . . . . 4-8
Simple notifications . . . . . . . . . . . . 4-8
Event-specific handlers . . . . . . . . . . 4-8
Returning information from
the handler . . . . . . . . . . . . . . . . 4-8
Declaring the event . . . . . . . . . . . . . . 4-9
Event names start with “On” . . . . . . . 4-9
Calling the event . . . . . . . . . . . . . . . 4-9
Chapter 6
Using graphics in components 6-1
Overview of graphics . . . . . . . . . . . . . . 6-1
Using the canvas . . . . . . . . . . . . . . . . . 6-3
Working with pictures . . . . . . . . . . . . . 6-3
Using a picture, graphic, or canvas . . . . 6-4
Loading and storing graphics . . . . . . . 6-4
Handling palettes . . . . . . . . . . . . . . 6-5
Specifying a palette for a control . . . . 6-5
Off-screen bitmaps . . . . . . . . . . . . . . . 6-6
Creating and managing
off-screen bitmaps . . . . . . . . . . . . . 6-6
Copying bitmapped images . . . . . . . . 6-7
Responding to changes . . . . . . . . . . . . . 6-7
Chapter 7
Handling messages and system
notifications
7-1
Understanding the message-handling
system . . . . . . . . . . . . . . . . . . . . . . 7-2
What’s in a Windows message? . . . . . . 7-2
Dispatching messages . . . . . . . . . . . . 7-3
Tracing the flow of messages . . . . . . 7-3
Changing message handling . . . . . . . . . . 7-4
Overriding the handler method . . . . . . 7-4
Using message parameters . . . . . . . . . 7-4
Trapping messages . . . . . . . . . . . . . . 7-5
Creating new message handlers . . . . . . . . 7-6
Defining your own messages . . . . . . . . 7-6
Declaring a message identifier . . . . . 7-6
Declaring a message-record type . . . . 7-6
Declaring a new message-handling
method . . . . . . . . . . . . . . . . . . . 7-7
Sending messages . . . . . . . . . . . . . . 7-8
Broadcasting a message to all
controls in a form . . . . . . . . . . . . 7-8
Calling a control’s message
handler directly . . . . . . . . . . . . . 7-9
Sending a message using the
Windows message queue . . . . . . . 7-9
Sending a message that does not
execute immediately . . . . . . . . . 7-10
Chapter 5
Creating methods 5-1
Avoiding dependencies . . . . . . . . . . . . . 5-1
Naming methods . . . . . . . . . . . . . . . . . 5-2
Protecting methods . . . . . . . . . . . . . . . . 5-2
Methods that should be public . . . . . . . 5-3
Methods that should be protected . . . . . 5-3
Abstract methods . . . . . . . . . . . . . . . 5-3
Making methods virtual . . . . . . . . . . . . . 5-4
Declaring methods . . . . . . . . . . . . . . . . 5-4
iv
Responding to system notifications
using CLX . . . . . . . . . . . . . . . . . . . 7-10
Responding to signals . . . . . . . . . . . 7-11
Assigning custom signal handlers . . . 7-11
Responding to system events . . . . . . . 7-12
Commonly used events . . . . . . . . . 7-13
Overriding the EventFilter
method . . . . . . . . . . . . . . . . . 7-15
Generating Qt events . . . . . . . . . . 7-15
Chapter 9
Modifying an existing component 9-1
Creating and registering the component . . . 9-1
Modifying the component class . . . . . . . . 9-2
Overriding the constructor . . . . . . . . . 9-2
Specifying the new default
property value . . . . . . . . . . . . . . . 9-3
Chapter 10
Creating a graphic control
Chapter 8
Making components available
at design time 8-1
Registering components . . . . . . . . . . . . . 8-1
Declaring the Register procedure . . . . . . 8-2
Writing the Register procedure . . . . . . . 8-2
Specifying the components . . . . . . . . 8-3
Specifying the palette page . . . . . . . . 8-3
Using the RegisterComponents
function . . . . . . . . . . . . . . . . . . 8-3
Providing Help for your component . . . . . . 8-4
Creating the Help file . . . . . . . . . . . . . 8-4
Creating the entries . . . . . . . . . . . . 8-4
Making component Help
context-sensitive . . . . . . . . . . . . . 8-6
Adding component Help files . . . . . . 8-6
Adding property editors . . . . . . . . . . . . . 8-6
Deriving a property-editor class . . . . . . . 8-7
Editing the property as text . . . . . . . . . 8-8
Displaying the property value . . . . . . 8-8
Setting the property value . . . . . . . . 8-9
Editing the property as a whole . . . . . . 8-10
Specifying editor attributes . . . . . . . . 8-11
Registering the property editor . . . . . . 8-12
Property categories . . . . . . . . . . . . . . . 8-13
Registering one property at a time . . . . 8-13
Registering multiple properties
at once . . . . . . . . . . . . . . . . . . . 8-14
Specifying property categories . . . . . . 8-14
Using the IsPropertyInCategory
function . . . . . . . . . . . . . . . . . . . 8-15
Adding component editors . . . . . . . . . . 8-16
Adding items to the context menu . . . . 8-16
Specifying menu items . . . . . . . . . 8-16
Implementing commands . . . . . . . 8-17
Changing the double-click behavior . . . 8-18
Adding clipboard formats . . . . . . . . . 8-19
Registering the component editor . . . . . 8-19
Compiling components into packages . . . . 8-20
10-1
Creating and registering the
component . . . . . . . . . . . . . . . . . . 10-1
Publishing inherited properties . . . . . . . 10-2
Adding graphic capabilities . . . . . . . . . 10-3
Determining what to draw . . . . . . . . 10-3
Declaring the property type . . . . . . 10-3
Declaring the property . . . . . . . . . 10-4
Writing the implementation
method . . . . . . . . . . . . . . . . . 10-4
Overriding the constructor
and destructor . . . . . . . . . . . . . . 10-5
Changing default
property values . . . . . . . . . . . . 10-5
Publishing the pen and brush . . . . . . 10-6
Declaring the class fields . . . . . . . 10-6
Declaring the access properties . . . . 10-6
Initializing owned classes . . . . . . . 10-7
Setting owned classes’ properties . . . 10-8
Drawing the component image . . . . . . 10-9
Refining the shape drawing . . . . . . . . 10-10
Chapter 11
Customizing a grid 11-1
Creating and registering the component . . . 11-1
Publishing inherited properties . . . . . . . . 11-3
Changing initial values . . . . . . . . . . . . . 11-4
Resizing the cells . . . . . . . . . . . . . . . . . 11-4
Filling in the cells . . . . . . . . . . . . . . . . 11-6
Tracking the date . . . . . . . . . . . . . . . 11-6
Storing the internal date . . . . . . . . . 11-7
Accessing the day, month,
and year . . . . . . . . . . . . . . . . . 11-7
Generating the day numbers . . . . . . 11-9
Selecting the current day . . . . . . . 11-11
Navigating months and years . . . . . . . . 11-11
v
Zgłoś jeśli naruszono regulamin