RAD Studio
ContentsIndex
PreviousUpNext
Introduction to component creation
Name 
Description 
This set of topics 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.
The main topics discussed are For information on installing new components, see Installing component packages
Delphi's components reside in the Visual Component Library (VCL) . The following figure shows the relationship of selected classes that make up the VCL hierarchy. For a more detailed discussion of class hierarchies and the inheritance relationships among classes, see Object-oriented programming for component writers
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.

When you create a component, you add to the component library by deriving a... more 
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. 
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: The following table summarizes the different kinds of components and the classes you use as starting points for each.
Component creation starting points  
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.
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)... more 
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. Each widget-based control has a handle, accessed through its Handle property, that identifies the underlying widget.-->
All windowed controls descend from the TWinControlclass. These include most standard windowed controls, such as pushbuttons, list boxes, and edit... more 
This topic describes how to create and setup a component. 
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. 
Each new component must have a constructor that overrides the constructor of the class from which it was derived. When you write the constructor for your new component, it must always call the inherited constructor.
Within the class declaration, declare a virtual constructor in the public section of the class.
For example,  
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. The section Creating components describes which class to derive different kinds of components from.
Deriving classes is explained in more detail in The section Defining new classes.
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
Nonvisual components are used as interfaces for elements like databases (TDataSet or TSQLConnection) and system clocks (TTimer), and as placeholders for dialog boxes (TCommonDialog 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. 
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... more 
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... more 
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 section devoted to it in this file, but the discussion that follows explains some of the motivation for their use. 
Registration is a simple process that tells the IDE which components to add to its component library, and on which pages of the Tool palette they should appear. For a more detailed discussion of the registration process, see Making components available at design time 
One quality that makes components usable is the absence of restrictions on what they can do at any point in their code. By their nature, components are incorporated into applications in varying combinations, orders, and contexts. You should design components that function in any situation, without preconditions.
An example of removing dependencies is the Handle property of TWinControl. If you have written Windows applications before, you know that one of the most difficult and error-prone aspects of getting a program running is making sure that you do not try to access a windowed control until you have created it... more 
Before you can install your components in the IDE, you have to register them. Registration tells Delphi where to place the component on the Tool palette. You can also customize the way Delphi stores your components in the form file. For information on registering a component, see Registering components. 
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... more 
The Component wizard simplifies the initial stages of creating a component. When you use the Component wizard, you need to specify:
  • The class from which the component is derived.
  • The class name for the new component.
  • The Tool palette category where you want it to appear.
  • The name of the unit in which the component is created.
  • The search path where the unit is found.
  • The name of the package in which you want to place the component.
The Component wizard performs the same tasks you would when creating a component manually:
  • Creating a unit.
  • Deriving the component.
  • Registering the... more 
To make your components reliable parts of the Delphi environment, you need to follow certain conventions in their design. This section discusses the following topics:  
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 ToolsOptions, navigate to the Environment OptionsDelphi OptionsLibrary page). The .lib files are placed in the DCP output directory. If adding the component entails creating a new package (as opposed to... more 
You can test the design-time behavior of a component after you install it on the Tool 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 Tool palette. For information on testing components that have not yet been installed, see Testing uninstalled components.
Testing your components after installing allows you to debug the component that only generates design-time exceptions when dropped on a form. 
You can test the runtime behavior of a component before you install it on the Tool 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 Tool palette. For information on testing already installed components, see Testing installed components.
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. 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!