此处所列的文章均是我自己从国外的网站摘抄并翻译的,由于英文水平有限,里面肯定有不少错漏.翻译这些东西没有其他的什么用途,只是提高自己的英语阅读能力和编程技术水平而已     

Delphi7组件编写者指南(翻译)

verview of component creation
组件创建概述
This chapter provides an overview of component design and the process of writing
components for Delphi applications. The material here assumes that you are familiar
with Delphi and its standard components.
本章提供一个组件设计和用Delphi应用程序开发组件的概述。本资料这里假设你熟悉了Delphi语言和它的标准组件。
• Class library
• Components and classes
• Creating components
• What goes into a component?
• Creating a new component
• Testing uninstalled components
• Testing installed components
• Installing a component on the Component palette
For information on installing new components, see “Installing component packages”
on page 16-10 of the Developer’s Guide.
• 类库
• 组件和类
• 创建组件
• 研究组件的什么
• 创建一个新的组件
• 测试未安装的组件
• 测试已经安装的组件
• 安装一个组件到组件面板上
更多安装新组件的信息,请参见"Installing component packages" 开发者指南16-10.
For information on installing new components, see “Installing component packages”
on page 16-10 of the Developer’s Guide.

Class library
类库
Delphi’s components reside in a component library that includes the Visual
Component Library (VCL) and the Component Library for Cross-Platform (CLX).
Figure 1.1 shows the relationship of selected classes that make up the VCL hierarchy.
The CLX hierarchy is similar to the VCL hierarchy but Windows controls are called
widgets (therefore TWinControl is called TWidgetControl, for example), and there are
other differences. For a more detailed discussion of class hierarchies and the
inheritance relationships among classes, see Chapter 2, “Object-oriented
programming for component writers.” For an overview of how the hierarchies differ
from each other, see “WinCLX versus VisualCLX” on page 15-7 of the Developer’s
Guide and refer to the CLX online reference for details on the components.
Delphi的组件属于一个包含可视化和跨平台的组件库。
图1.1显示了组成VCL组织结构的类的关系。CLX的组织结构类似于VCL的组织结构0但是Windows controls被称作widgets(因此TWinControl 叫做 TWidgetControl,例如)还有其他的不同点。更多的详细关于这些类的组织结构和继承关系的讨论,参看第二章。"组件编写者面向对象的编程"。对于两者组织结构的如何不同,参见开发者指南第15-7"WinCLX 与 VisualCLX的比较"和参阅CLX在线关于组件细节的参考书目。
The TComponent class is the shared ancestor of every component in the component
library. TComponent provides the minimal properties and events necessary for a
component to work in the IDE. The various branches of the library provide other,
more specialized capabilities.
TComponent类是组件库中每一个组件共享的基类。TComponent提供很少的供IDE工作的属性和事件。库的其他分支提供其他,更多特别的能力。
When you create a component, you add to the component library by deriving a new
class from one of the existing class types in the hierarchy.
当你创建一个组件,添加到组件库通过从已经存在于这个组织结构的类类型派生一个新的类。
组件和类
Because components are classes, component writers work with objects at a different
level from application developers. Creating new components requires that you
derive new classes.
因为组件是类,从应用程序开发者的角度来说组件开发者使用这些象处在一个不同的水平,
创造一个新组件要求你派生一个新的类。
Briefly, there are two main differences between creating components and using them
in applications. When creating components,
• You access parts of the class that are inaccessible to application programmers.
• You add new parts (such as properties) to your components.
Because of these differences, you need to be aware of more conventions and think
about how application developers will use the components you write.
简单地说,在组件编写者和在程序中使用它们有2个主要的不同。当你创建组件的时候。
你可以访问那些不能应用程序员不能访问的类
你增加新的部分(比如属性)到你的组件中。
因为这不同,你需要考虑更多的便利和思索应用程序员怎么样使用你写的组件。

Creating components
创建组件
A component can be almost any program element that you want to manipulate at
design time. Creating a component means deriving a new class from an existing one.
You can derive a new component in several ways:
一个组件可能是你想在设计时操纵的任何元素程序元素。创建一个组件意味着从一个存在的类中派生一个新的类。
你可以通过几种方式派生一个新的组件:
• Modifying existing controls
• Creating windowed controls
• Creating graphic controls
• Subclassing Windows controls
• Creating nonvisual components
修改一个已经存在的控件
创建一个windowed的控件
创建一个图形控件
子类化window控件
创建一个非可视化组件
Table 1.1 summarizes the different kinds of components and the classes you use as
starting points for each.
表1.1概括了不同种类的组件和你用它作为起点的类
Modify an existing component Any existing component, such as TButton or TListBox, or
an abstract component type, such as TCustomListBox
修改一个存在的组件任何存在的组件,比如TButton 或者TListBox,或者一个抽象的组件类型,比如TCustomListBox.
Create a windowed (or widget-
based in CLX applications) control
TWinControl (TWidgetControl in CLX applications)
创建一个Windowed(或者在CLX应用程序中基于widget)的控件TWinControl (TWidgetControl存在于CLX 应用程序中)
Create a graphic control TGraphicControl
创建一个图形控件TGraphicControl
Subclassing a control Any Windows (VCL applications) or widget-based (CLX
applications) control
子类一个控件任何Windows(VCL应用程序)或者
Create a nonvisual component TComponent基于widget(CLX应用程序中)的控件


You can also derive classes that are not components and cannot be manipulated on a
form, such as TRegIniFile and TFont.
你也可以派生自不是组件并且不能在窗体上被操作的类,比如 TRegIniFile and TFont
Modifying existing controls
修改存在的控件
The simplest way to create a component is to customize an existing one. You can
derive a new component from any of the components provided in the component
library.
最常见的创建组件的方式是定制一个存在的组件。你可以从组件库中的任何组件派生出一个新的组件。
Some controls, such as list boxes and grids, come in several variations on a basic
theme. In these cases, the component library includes an abstract class (with the word
“custom” in its name, such as TCustomGrid) from which to derive customized
versions.
一些控件,比如ListBoxe和Grid,在基本的主题上利用了一些变化。在这种情况下,组件库包含抽象类(在名字中使用custom,比如TCustomGrid)表示从那里派生自定义的版本。
例如,你可能想去创建一个特定的ListBox但是不想有一些标准的TListBox类的属性.你不能删除(隐藏)一个从祖先类继承的属性,所以你需要去从层次结构上的TListBox派生你的组件。好过强制你从一个抽象的 TWinControl(或者在 CLX 应用程序中是TWidgetControl)类并且彻底改造所有的ListBox的功能,组件库提供了一个实现了一个TListBox的属性但是没有发布所有属性的TCustomListBox,当你从抽象类像 TCustomListBox等派生一个组件,你发布那些你想在你的组件中可以用的,让剩余部分被保护起来。
第三章, “Creating properties,” 解释了发布的继承属性.
第九章, "Modifying an existing component" 和第十一章"Customizing a
grid", 展示修改存在控件的例子。
,For example, you might want to create a special list box that does not have some of
the properties of the standard TListBox class. You cannot remove (hide) a property
inherited from an ancestor class, so you need to derive your component from
something above TListBox in the hierarchy. Rather than force you to start from the
abstract TWinControl (or TWidgetControl in CLX applications) class and reinvent all
the list box functions, the component library provides TCustomListBox, which
implements the properties of a list box but does not publish all of them. When you
derive a component from an abstract class like TCustomListBox, you publish only the
properties you want to make available in your component and leave the rest
protected.
Chapter 3, “Creating properties,” explains publishing inherited properties.
Chapter 9, “Modifying an existing component,” and Chapter 11, “Customizing a
grid,” show examples of modifying existing controls.

Creating windowed controls
创建windows控件
Windowed controls in the component library are objects that appear at runtime and
that the user can interact with. Each windowed control has a window handle,
accessed through its Handle property, that lets the operating system identify and
operate on the control. If using VCL controls, the handle allows the control to receive
input focus and can be passed to Windows API functions. CLX controls are widget-
based controls. Each widget-based control has a handle, accessed through its Handle
property, that identifies the underlying widget.
All windowed controls descend from the TWinControl (TWidgetControl in CLX) class
These include most standard windowed controls, such as pushbuttons, list boxes,
and edit boxes. While you could derive an original control (one that’s not related to
any existing control) directly from TWinControl (TWidgetControl in CLX), Delphi
provides the TCustomControl component for this purpose. TCustomControl is a
specialized windowed control that makes it easier to draw complex visual images.
Chapter 11, “Customizing a grid,” presents an example of creating a windowed
control.
组件库中的windows控件是在运行时可见并且用户可以交互的对象。每一个windows控件有一个句柄,通过它的Handle属性来访问,
让操作系统标识和操纵控件,如果使用VCL控件,Handle可以让控件收到输入焦点并且可以被传递到windows API函数.CLX控件是基于widget的控件.每一个
基于widget控件都有一个句柄,通过访问句柄来标识下层的widget.所有的windows控件衍生自标准的windows控件,比如pushbuttons, list boxes,
和 edit boxes,
Creating graphic controls
If your control does not need to receive input focus, you can make it a graphic
control. Graphic controls are similar to windowed controls, but have no window
handles, and therefore consume fewer system resources. Components like TLabel,
which never receive input focus, are graphic controls. Although these controls cannot
receive focus, you can design them to react to mouse messages.
You can create custom controls through the TGraphicControl component.
TGraphicControl is an abstract class derived from TControl. Although you can derive
controls directly from TControl, it is better to start from TGraphicControl, which
provides a canvas to paint on and on Windows, handles WM_PAINT messages; all
you need to do is override the Paint method.
Chapter 10, “Creating a graphic control,” presents an example of creating a graphic
control.
创建图形控件
如果你的控件不需要接受输入焦点,你可以使它作为一个图形控件。图形控件类似于windows控件,但是没有窗口句柄, 因此花费更少的系统资源,尽管这些控件不能接受焦点,你能设计它们能够接受鼠标事件,你能通过TGraphicControl组件创建自定义的控件。
TGraphicControl是一个继承于TControl的抽象类,尽管你能直接从TControl派生一个类,但是最好是从TGraphicControl开始,它提供一个canvas去绘制,处理WM_PAINT消息,所有你需要去做的是重载Paint方法。
第10章,"创建一个图形控件",展示一个创建图形控件的例子。

Subclassing Windows controls
子类化Windows控件
In traditional Windows programming, you create custom controls by defining a new
window class and registering it with Windows. The window class (which is similar to
the objects or classes in object-oriented programming) contains information shared
among instances of the same sort of control; you can base a new window class on an
existing class, which is called subclassing. You then put your control in a dynamic-
link library (DLL), much like the standard Windows controls, and provide an
interface to it.
You can create a component “wrapper” around any existing window class. So if you
already have a library of custom controls that you want to use in Delphi applications,
you can create Delphi components that behave like your controls, and derive new
controls from them just as you would with any other component.
For examples of the techniques used in subclassing Windows controls, see the
components in the StdCtls unit that represent standard Windows controls, such as
TEdit. For CLX applications, see QStdCtls.
在传统的windows编程中,你能通过定义一个新的window类来创建一个控件,并且通过windows来注册它。windows类(类似于对象或者面向对象编程中的类)包含相同控件所有实例的共有信息。你可以把一个新的Windows类建立在一个存在的类的基础上,这叫做子类化,你可以把你自己的控件放入一个动态链接库中,十分像标准的windows控件,并且提供一个接口给它。你能围绕一个存在的类创建一个叫"wrapper"的组件。所以如果你已经有了一个在delphi应用程序中使用的自定义的控件库,你可以创建Delphi控件就像你创建自己的控件一样,,并且通过它们派生一个新的控件就好象你已经有了任何其他的控件一样。
关于使用子类化windows控件的技术例子,参考StdCtls单元中实现标准windows控件的组件,比如TEddit,对于CLX应用程序,参见QStdCtls.
Creating nonvisual components
//创建非可视化的组件
Nonvisual components are used as interfaces for elements like databases (TDataSet or
TSQLConnection) and system clocks (TTimer), and as placeholders for dialog boxes
(TCommonDialog (VCL applications) or TDialog (CLX applications) and its
descendants). Most of the components you write are likely to be visual controls.
Nonvisual components can be derived directly from TComponent, the abstract base
class for all components.
非可视化的组件
非可视化的组件被用作元素的接口像databases (TDataSet or
TSQLConnection) 和system clocks (TTimer),作为对话框(TCommonDialog (VCL应用程序) or TDialog (CLX 应用程序)和它的衍生)占位符,你写的大部分组件是可视化的组件,非可视化的组件可以直接从TComponent派生,是所有组件的抽象基类。

 

By removing background tasks like creating the window, Delphi components allow
developers to focus on what they really want to do. Before passing a window handle
to an API function, you do not need to verify that the handle exists or to create the
window. The application developer can assume that things will work, instead of
constantly checking for things that might go wrong.
Although it can take time to create components that are free of dependencies, it is
generally time well spent. It not only spares application developers from repetition
and drudgery, but it reduces your documentation and support burdens.
通过移除后台任务像创建窗口等,Delphi组件让开发者把焦点放在他们真正想做的事情上。当传递一个windows 句柄给一个API函数,你不必需要去检查句柄的存在或者去创建一个窗体,那应用程序开发者能假设所有的事情将正确的运转,而不是时常地去检查这些可能出错的事情。
尽管可能花费时间去创建无依赖性的组件,通常是用得其所.它不仅能够避免应用才程序开发者重复和单调沉闷的工作,并且能够减少程序文档和支持的重负。

Setting properties, methods, and events
Aside from the visible image manipulated in the Form designer, the most obvious
attributes of a component are its properties, events, and methods. Each of these has a
chapter devoted to it in this book, but the discussion that follows explains some of
the motivation for their use.
设置属性,方法,事件
除了在Form设计器上被操纵的可见的图象外,最常见的组件属性是它的属性,事件和方法,本书每个都有一章专门的去介绍。
但是解释之后讨论一些使用它们的诱因。

Properties
Properties give the application developer the illusion of setting or reading the value
of a variable, while allowing the component writer to hide the underlying data
structure or to implement special processing when the value is accessed.
There are several advantages to using properties:
• Properties are available at design time. The application developer can set or
change initial values of properties without having to write code.
• Properties can check values or formats as the application developer assigns them.
Validating input at design time prevents errors.
• The component can construct appropriate values on demand. Perhaps the most
common type of error programmers make is to reference a variable that has not
been initialized. By representing data with a property, you can ensure that a value
is always available on demand.
• Properties allow you to hide data under a simple, consistent interface. You can
alter the way information is structured in a property without making the change
visible to application developers.
Chapter 3, “Creating properties,” explains how to add properties to your
components.
属性
属性给开发者一个设置和读一个变量的假象,当一个值被访问的时候,允许一个组件开发者去隐藏背后的数据结构或者去实现特殊的处理。
使用属性有如下的几个优点:
.属性在设计时是可用的.应用程序开发员能够设置或改变属性的初始值而不必去写代码。
.当程序开发者设置属性的时候属性能够检查值或者数据的格式。
.组件可以根据要求构造适当的值。可能最常见的程序员范的错误是引用一个没有初始化的变量.通过属性来表示数据,你可以确定根据要求一个值总是可用。
.属性允许你在简单地,始终如一的接口下隐藏数据。你可以改变信息的组织方式而不使改变让应用程序开发者可见。
第3章,"创建属性".解释了怎么样去添加属性到你的组件中。

Methods
Class methods are procedures and functions that operate on a class rather than on
specific instances of the class. For example, every component’s constructor method
(Create) is a class method. Component methods are procedures and functions that
operate on the component instances themselves. Application developers use
methods to direct a component to perform a specific action or return a value not
contained by any property.
Because they require execution of code, methods can be called only at runtime.
Methods are useful for several reasons:
• Methods encapsulate the functionality of a component in the same object where
the data resides.
• Methods can hide complicated procedures under a simple, consistent interface. An
application developer can call a component’s AlignControls method without
knowing how the method works or how it differs from the AlignControls method
in another component.
• Methods allow updating of several properties with a single call.
Chapter 5, “Creating methods,” explains how to add methods to your components.

方法
类方法是类上的一个过程或者函数而不是类的一个特定的实例.比如,每一个组件的构造函数是一个类方法.
组件方法是作用于组件实例自己的过程和函数.程序开发者用方法对组件执行特定的行为或者返回一个不包含任何属性的值。
方法需要执行的代码,方法只能在运行时被调用。
方法是有用的由于以下的几点理由:
方法概括了相同对象组件内数据的功能。
方法可以在一个普通,一致的接口下隐藏复杂的过程。
一个应用程序开发者可以调用一个组件的AlignControls方法而不用知道这个方法是如何工作或者它与另一个组件的AlignControls的区别。
方法允许通过一个单独的调用去更新一写属性。
第五章,"Creating methods",讲解了怎么样把一个方法添加到你的组件中。

Events
An event is a special property that invokes code in response to input or other activity
at runtime. Events give the application developer a way to attach specific blocks of
code to specific runtime occurrences, such as mouse actions and keystrokes. The code
that executes when an event occurs is called an event handler.
Events allow application developers to specify responses to different kinds of input
without defining new components.
Chapter 4, “Creating events,” explains how to implement standard events and how
to define new ones.

事件是一个特殊的属性,对运行时输入或其他的活动调用反映的代码。
事件给开发者关联一个特殊的代码到运行时事件的方法。比如鼠标动作和敲击键盘,当一个事件发生时执行的代码被称为事件处理器
事件允许应用程序开发者去响应不同种类输入。
第四章,"Creating events",讲解了怎么样去实现标准事件和如何去定义一个新的事件。

Encapsulating graphics
Delphi simplifies Windows graphics by encapsulating various graphics tools into a
canvas. The canvas represents the drawing surface of a window or control and
contains other classes, such as a pen, a brush, and a font. A canvas is like a Windows
device context, but it takes care of all the bookkeeping for you.
If you have written a graphical Windows application, you are familiar with the
requirements imposed by Windows’ graphics device interface (GDI). For example,
GDI limits the number of device contexts available and requires that you restore
graphic objects to their initial state before destroying them.
With Delphi, you do not have to worry about these things. To draw on a form or
other component, you access the component’s Canvas property. If you want to
customize a pen or brush, you set its color or style. When you finish, Delphi disposes
of the resources. Delphi caches resources to avoid recreating them if your application
frequently uses the same kinds of resource.
封装图形
delphi简化了windows的图形通过封装各种图形工具到一个Canvas中。Canvas实现了在一个windows或控件的表面绘制和其它的类,
比如Pen,Font, 一个Canvas就像windows设备上下文,但是它会为你保管好所有的簿记。
如果你写过一个windows图形应用程序,你对那些被GDI强行的要求应该很熟悉,举个例子,GDI限制可用的设备上下文的数量并且你恢复图形对象到初始状态在销毁它们之前。
使用Delphi,你不必担心这些事情,在一个Form或者其他的组件上绘制,你访问组件的Canvas属性,如果你想去自定义一个Pen或者Brush,设置它的颜色或者样式。当你完成,Delphi释放那些资源,如果你的程序频繁地使用相同类型的资源Delphi缓存资源避免重复创建他们。
You still have full access to the Windows GDI, but you will often find that your code
is simpler and runs faster if you use the canvas built into Delphi components.
CLX graphics encapsulation works differently. A canvas is a painter instead. To draw
on a form or other component, you access the component’s Canvas property. Canvas
is a property and it is also an object called TCanvas. TCanvas is a wrapper around a Qt
painter that is accessible through the Handle property. You can use the handle to
access low-level Qt graphics library functions.
If you want to customize a pen or brush, you set its color or style. When you finish,
Delphi or Kylix disposes of the resources. CLX applications also cache the resources.
You can use the canvas built into CLX components by descending from them. How
graphics images work in the component depends on the canvas of the object from
which your component descends.Graphics features are detailed in Chapter 6, “Using
graphics in components.”
你仍然可以完全地访问windows GDI,但是如果你用Delphi内部的Canvase,你将常常发现你的代码更简单,运行得更快。
CLX图形封装不同,Canvas代替了Canvas,Canvas是一个属性同时也是一个称为TCanvase的对象,TCanvas是在可用Handle句柄访问的Qt Painter的一层包装纸。你可以用这个句柄去访问Qt 底层的函数库。
如果你想去自定义一个Pen或者画刷,设置它的颜色或者样式,当你完成的时候,Delphi或Kylix释放资源,CLX应用程序也缓存资源,
通过继承它们你能使用成为CLX组件一部分的Canvas。组件内的图形图象怎么样工作依赖于你从那个组件去派生。
图象功能详细信息在第6章,"在组件中使用图形".
Registering components
Before you can install your components in the IDE, you have to register them.
Registration tells Delphi where to place the component on the Component palette.
You can also customize the way Delphi stores your components in the form file. For
information on registering a component, see Chapter 8, “Making components
available at design time.”
注册组件
在你可以安装组件到你的IDE之前,你不得不去注册它们。注册告诉Delphi组件在组件面板中的位置。
你也可以自定义Delphi存放你的组件在Form文件中的方法,更多的注册组件的信息,参考第8章,"Making components
available at design time"
Creating a new component
You can create a new component two ways:
• Creating a component with the Component wizard
• Creating a component manually
You can use either of these methods to create a minimally functional component
ready to install on the Component palette. After installing, you can add your new
component to a form and test it at both design time and runtime. You can then add
more features to the component, update the Component palette, and continue
testing.
There are several basic steps that you perform whenever you create a new
component. These steps are described below; other examples in this document
assume that you know how to perform them.
1 Create a unit for the new component.
2 Derive your component from an existing component type.
3 Add properties, methods, and events.
4 Register your component with the IDE.
你可以创建新的组件的两种方法:
创建组件使用组件向导。
手动创建一个组件
你可以使用这两种中的一种去创建一个最少功能的组件安装到组件面板中。安装完后,你可以添加新的组件到一个窗体上并且在运行时和设计时测试,你也可以添加更多的功能到你的组件上,更新组件面板,继续测试。
这里有几个基本的步骤去创建一个新的组件。以下描述这些步骤,其它在这个文档中的的例子建设你知道如何去操作它们。
1.为组件创建一个新的单元
2.从已经存在的组件类型中派生一个新的组件
3.增加属性,方法和事件
4.用IDE注册你的组件。
5.为组件创建一个位图
6.创建一个包(一个特殊的动态链接库)以使你能安装你的组件到IDE中。
7.为你的组件和它的属性,方法,事件创建一个帮助文件。
注意:创建一个帮助文件去指导组件用户是可选的。
当你完成,那完成的组件包含以下的文件.
5 Create a bitmap for the component.
6 Create a package (a special dynamic-link library) so that you can install your
component in the IDE.
7 Create a Help file for your component and its properties, methods, and events.
Note Creating a Help file to instruct component users on how to use the component is
optional.
When you finish, the complete component includes the following files:
• A package (.BPL) or package collection (.DPC) file
• A compiled package (.DCP) file
• A compiled unit (.DCU) file
• A palette bitmap (.DCR) file
• A Help (.HLP) file
1.一个包package(.BPL)或者包集package collection(.DPC)文件
2.一个编译的包文件(.DCP)
3.一个编译的单元文件(.DCU)
4.一个位图面板(.DCR)文件
5.一个帮助文件。
You can also create a bitmap to represent your new component. See “Creating a
bitmap for a component” on page 1-13.
The chapters in the rest of Part V explain all the aspects of building components and
provide several complete examples of writing different kinds of components.
你可以去创建一个位图去实现你的新组件,参见"为组件创建一个位图"在1-13页
本章的剩余第五部分讲解创建组件的所有方面并且提供几个完整的写的几个不同种类组件的例子。

 

 

 

 

 

 

 

 

Creating a component manually
The easiest way to create a new component is to use the Component wizard. You can,
however, perform the same steps manually.
To create a component manually, follow these steps:
1 Creating a unit file
2 Deriving the component
3 Registering the component
手动创建组件
最容易的创建组件的方法是使用组件向导。但是,你仍然可以手动做相同的动作.
手动的创建组件,遵照这些步骤:
1创建一个单元文件
2派生一个组件
3注册一个组件
Creating a unit file
A unit is a separately compiled module of Delphi code. Delphi uses units for several
purposes. Every form has its own unit, and most components (or groups of related
components) have their own units as well
When you create a component, you either create a new unit for the component or add the new component to an existing unit.
To create a new unit for a component:
创建一个单元文件
一个单元是一个被单独编译的Delphi代码模块。Delphi使用单元为了几个目的。每一个Form有它自己的单元,并且大部分组件(或一组相关的组

件)也有它们自己的单元。
当你创建一个组件,你也为该组件创建了一个新的单元或者添加新组件到存在的单元。
为一个组件创建新的单元:
1 Choose either:
? File|New|Unit.
? File|New|Other to display the New Items dialog box, select Unit, and choose
OK.
The IDE creates a new unit file and opens it in the Code editor.
2 Save the file with a meaningful name.
3 Derive the component class.
To open an existing unit:
1 Choose File|Open and select the source code unit to which you want to add your
component.
When adding a component to an existing unit, make sure that the unit contains
only component code. For example, adding component code to a unit that contains
a form causes errors in the Component palette.
2 Derive the component class.
1:选择任意一个
? File|New|Unit.
? File|New|Other 显示一个新的项目对话框,选择单元,然后选择ok
IDE创建一个单元文件并且在代码编辑器中打开它。
使用易于理解的名字保存文件。
3.派生存在的组件
1.打开一个存在的单元:
1选择File|Open 并且选择你想去添加组件的源代码单元。
当你添加一个组件到存在的单元,确认那个单元只包含组件代码,比如,在组件面板中,添加组件到包含一个Form的单元将导致一个错误。
派生组件类。

 

派生组件类。
Deriving the component
Every component is a class derived from TComponent, from one of its more
specialized descendants (such as TControl or TGraphicControl), or from an existing
component class. “Creating components” on page 1-3 describes which class to derive
different kinds of components from.
Deriving classes is explained in more detail in the section “Defining new classes” on
page 2-2.
To derive a component, add an object type declaration to the interface part of the
unit that will contain the component.
A simple component class is a nonvisual component descended directly from
TComponent.
To create a simple component class, add the following class declaration to the
interface part of your component unit:
type
TNewComponent = class(TComponent)
end;
So far the new component does nothing different from TComponent. You have created
a framework on which to build your new component.
派生组件
组件是一个派生自TComponent的类, 从它的一个或多个特定的后代(比如TControl或者

TGraphicControl), 或者一个存在的组件类。
在1-3页 "创建组件"描述了派生自不同的组件。
讲解派生的类的更多的详述在2-2页的"派生新类"一节中。
派生一个组件,添加一个对象类型的声明到包含组件的单元中。
创建一个普通的组件类,添加如下的类的声明到你的组件单元的接口部分:
type
TNewComponent = class(TComponent)
end;
到目前为止新的组件同TComponent没有什么不同.你已经为创建你的新组件建立了一个框架。
Creating a new component
 Registering the component
Registration is a simple process that tells the IDE which components to add to its
component library, and on which pages of the Component palette they should
appear. For a more detailed discussion of the registration process, see Chapter 8,
“Making components available at design time.”
To register a component:
1 Add a procedure named Register to the interface part of the component’s unit.
Register takes no parameters, so the declaration is very simple:
procedure Register;
If you are adding a component to a unit that already contains components, it
should already have a Register procedure declared, so you do not need to change
the declaration.
Note Although Delphi is a case insensitive language, the Register procedure is case
sensitive and must be spelled with an uppercase R.
2 Write the Register procedure in the implementation part of the unit, calling
RegisterComponents for each component you want to register. RegisterComponents is
a procedure that takes two parameters: the name of a Component palette page and
a set of component types. If you are adding a component to an existing
registration, you can either add the new component to the set in the existing
statement, or add a new statement that calls RegisterComponents.
To register a component named TMyControl and place it on the Samples page of the
palette, you would add the following Register procedure to the unit that contains
TMyControl’s declaration:
procedure Register;
begin
RegisterComponents('Samples', [TNewControl]);
end;
This Register procedure places TMyControl on the Samples page of the Component
palette.
Once you register a component, you can compile it into a package (see Chapter 8,
“Making components available at design time”) and install it on the Component
palette.

注册组件
注册是一个告诉IDE哪一个组件被添加到它的组件库中,在哪些组件面板中出现的普通过程。对于更

多的关于注册过程的详细论述,参见第8章,"使组件在设计时可见".
注册一个组件:
1 增加一个叫做Register的过程到组件单元的接口部分。
Register不带参数,所以它的声明非常简单:
Register过程;
如果你添加组件到一个已经包含组件的单元,它应该已经有一个声明的Register过程,所以你不必去

改变生命。
注意:尽管Delphi是一个大小写不敏感的语言,那个注册过程是大小写敏感的并且必须拼为大写的R.
2:写Register过程到单元的实现部分,为每一个你想注册的组件调用RegisterComponents.

RegisterComponents是一个带2个参数的过程:组件面板的名字和组件类型的集合,你也可以添加新的

组件到已经存在的集合语句中,或者添加一个新的语句去调用RegisterComponents.
注册一个叫做TMyControl的组件然后放它到面板的Samples页,你应该增加如下的Register过程到
包含TMyControl的单元声明中
procedure Register;
begin
RegisterComponents('Samples', [TNewControl]);
end;
一旦你注册一个组件,你可以编译到一个包(参见第8章,"使组件在设计时可见")中并且安装到组件面

板中.

 

Installing a component on the Component palette
To install components in a package and onto the Component palette:
1 Choose Component|Install Component.
The Install Component dialog box appears.
2 Install the new component into either an existing or a new package by selecting
the applicable page.
3 Enter the name of the .pas file containing the new component or choose Browse to
find the unit.
4 Adjust the search path if the .pas file for the new component is not in the default
location shown.
5 Enter the name of the package into which to install the component or choose
Browse to find the package.
6 If the component is installed into a new package, optionally enter a meaningful
description of the package.
7 Choose OK to close the Install Component dialog box. This compiles/rebuilds the
package and installs the component on the Component palette.
Note Newly installed components initially appear on the page of the Component palette
that was specified by the component writer. You can move the components to a
different page after they have been installed on the palette with the Component|
Configure Palette dialog box.
For component writers who need to distribute their components to users to install on
the Component palette, see “Making source files available” on page 1-16.
安装组件到组件面板中,
安装包中的组件到组件面板中:
1选择Component|Install Component
2通过选择合适的页安装新的组件到已经存在的或一个新的包中。
3输入包含组件的单元名字或者选择浏览去找到那个单元
4.如果包含组件的.pas文件不在默认的显示位置需要调整搜索路径。
5.输入安装组件的包的名字或者选择浏览去找到包
6.如一个组件被安装进一个新的包,可选择一个易于理解的名字去描述包。
7.选择ok去关闭安装组件对话框。这将编译/重新建造包并且安装到组件面板上去。
注意:新安装的组件包将初始被安装到组件编写者指定的组件面板上。你可以把已经安装的组件移动到不同的页上,通过Component|
Configure Palette对话框。
对于需要把自己的组件分发到用户安装到组件面板上的组件开发者,参见1-16页的"Making source files available".
Making source files available
Component writers should make all source files used by a component should be
located in the same directory. These files include source code files (.pas) and
additional project files (.dfm/.xfm, .res, .rc, and .dcr).
The process of adding a component results in the creation of a number of files. These
files are automatically put in directories specified in the IDE environment options
(use the menu command Tools|Environment Options, navigate to the Library tab
page). The .lib files are placed in the DCP output directory. If adding the component
entails creating a new package (as opposed to installing it into an existing package),
the .bpl file is put in the BPL output directory.
使源文件可用
组件编写者应该使所有的被组件使用的资源文件在相同的目录找到,这些文件包含资源文件(.pas)和额外的工程文件(.dfm/.xfm, .res, .rc, and .dcr).
添加组件的过程导致一些文件,这些文件自动的放到IDE环境选项(使用菜单命令工具Tools|Environment options, 导航到Library页)指定的目录中..lib

文件被放到DCP目录中。如果添加的组件需要创建一个新的包(反对安装到一个存在的包中),.bpl文件将被放到BPL目录中。
Testing uninstalled components
You can test the runtime behavior of a component before you install it on the
Component palette. This is particularly useful for debugging newly created
components, but the same technique works with any component, whether or not it is
on the Component palette. For information on testing already installed components,
see “Testing installed components” on page 1-18.
You test an uninstalled component by emulating the actions performed by Delphi
when the component is selected from the palette and placed on a form.
To test an uninstalled component,
1 Add the name of component’s unit to the form unit’s uses clause.
2 Add an object field to the form to represent the component.
This is one of the main differences between the way you add components and the
way Delphi does it. You add the object field to the public part at the bottom of the
form’s type declaration. Delphi would add it above, in the part of the type
declaration that it manages.
Never add fields to the Delphi-managed part of the form’s type declaration. The
items in that part of the type declaration correspond to the items stored in the form
file. Adding the names of components that do not exist on the form can render
your form file invalid.
3 Attach a handler to the form’s OnCreate event.
4 Construct the component in the form’s OnCreate handler.
When you call the component’s constructor, you must pass a parameter specifying
the owner of the component (the component responsible for destroying the
component when the time comes). You will nearly always pass Self as the owner.
In a method, Self is a reference to the object that contains the method. In this case,
in the form’s OnCreate handler, Self refers to the form.
测试未安装的组件
你能在组件安装到组件面板前测试一个组件的运行时的行为。这对于调试新创建的组件非常有用。
相同的技术对于任何组件都有用,不管是否组件在组件面板上。更多的信息测试已经安装的组件,参见1-18页的"测试安装的组件".
当组件从组件面板上被选择和放到一个Form上,你测试一个未安装的组件通过模仿delphi的行为。
测试一个未安装的组件。
1增加新组件的名字到使用单元的"Unit"子句中。
2添加一个对象域到Form去显示组件。
这是在你添加组件的方式和Delphi的方式最主要的不同。你添加组件对象域到Form类型声明的底部的public部分,Delphi添加到上面,它自己控制的声明部分

。绝不要添加域到Delphi自己管理的Form的声明部分.
 

 

 

posted @ 2010-06-26 11:10  AppleAndPear  阅读(2651)  评论(1编辑  收藏  举报