All visual component library classes evolve from a group of base classes that underlie the VCL hierarchy. The most common ancestor of components is the TObject class, which encapsulates the simplest object. As you know (see Chapter 1), each object inherits the properties and methods of its parent class. You can add new properties and methods to an object, but you cannot delete inherited ones. The inherited object, in turn, can become the parent of a new class, which will inherit the capabilities of all its ancestors.
Therefore, the hierarchy of VCL base classes is thought out extremely carefully – because on their basis all the many components used in Delphi are created. A special place among the base classes, in addition to TObject, is occupied by TComponent (from which all components derive) and TControl (from which all controls derive).
This chapter discusses the hierarchy of base classes and their capabilities. The information presented here will help you understand the basic mechanisms of how components function. This chapter will serve as a reference for those who create their own objects and controls.
Delphi's diversity of classes and components is based on just five base classes (See Figure 2.1). They provide the basic functions of any object – whether it is a standard VCL component or a specialized object that performs some operations in the application.
Fig. 2.1. VCL Base Class Hierarchy
Thanks to the mechanism of inheritance of properties and methods, the descendants of the base classes are able to "communicate" with each other; Work in the development environment, interacting with the Component Palette and the Object Inspector. are recognized by the operating system as controls and windows.
The class hierarchy is based on the TObject class. It ensures the performance of the most important functions of the "vital activity" of any object. Thanks to him, each class inherits the mechanisms for creating an instance of an object and destroying it.
Usually, the developer does not even think about how the object will be created and what needs to be done to destroy it correctly. VCL components are created and free up occupied resources automatically. Sometimes a developer has to create and delete objects themselves. But even in this case, it is enough for him to call the appropriate constructor and destructor:
var SomeList: TStrings; … SomeList: = TStrings.Create; … SomeList.Free;
Behind the apparent simplicity of these operations lies a rather complex implementation of these processes. Almost all of the source code of the TObject class is written in assembly language to ensure the greatest efficiency of the operations that will be performed in each of its descendants.
In addition, the TObject class provides for the creation and storage of information about the object instance and the maintenance of message queuing.
The TPersistent class derives directly from the TObject class. It provides its descendants with the ability to interact with other objects and processes in the data layer. Its methods allow you to pass data to streams, and also ensure that the object interacts with the Object Inspector.
The TComponent class is essential for all components. You can create any non-visual components directly from it. The mechanisms implemented in the TComponent class enable the component to interact with the development environment, primarily the Component Palette and the Object Inspector. Thanks to the capabilities of this class, components begin to work on the project form already at the development stage.
The TControl class derives from the TComponent class. Its main purpose is to ensure the functioning of visual components. Each visual component derived from the TControl is endowed with basic features of the control. Thanks to this, each visual component is able to work with the GUI (Graphic User Interface) and display itself on the screen.
The TWinControl class extends the ability of developers to create controls. It inherits from the TControl class and enables the creation of windowed controls.
Based on the TWinControl class, another additional class has been created – TCustomControl. It provides the components created on its basis with the ability to use the canvas - a special object designed to display graphics (for more on canvas, see Chapter L).
The TCustomControl class is the common ancestor of a whole group of classes that enable you to create various non-standard types of Windows window (focus)controls: editors, lists, and so on.
To create non-windowed (non-focus) controls, use the TGraphicControl class, which is a child of the TControli class.
In general, the hierarchy of base classes allows developers to work fully in Delphi, allowing you to design any type of application.
Below we will focus on the basic properties and methods of the base classes, highlighting only those that can be useful in real work. Some of them are available in the Object Inspector, some can be used in program code.