How to write a component

How to write a component

The open Delphi coding environment is based on use of different components. The component represents the code which is carrying out any narrowly targeted task by means of properties, events and procedures. During creation of a component it is necessary to set values of variables and to implement the code of event handlers. For use in the program the new component should be included in a project package.

It is required to you

  • Delphi development environment.

Instruction

1. Select type of the created component. It can be an object of Windows element, a graphic element, control or a non-visual component. Also your object can be the successor of any already existing class. Decide on functions which implementation you will assign to a component.

2. Launch the Delphi development environment. In the main application menu open the points Component, New Component. In the appeared dialog box in the field of Ancestor Type select a class of a component which you want to modify. If you do not use inheritance, in the Class Name field just enter the name of a new component beginning on the letter ""T"". In the field of Palette Page write a component bookmark name after installation, further click Create Unit. Automatically the development environment will generate a template of a new component. An example of the formed code in the Pascal language:

unit MyBtn;
interface
uses
Windows, SysUtils, Messages, Classes, Controls, Graphics, Forms, StdCtrls, Dialogs;

type
TMyBtn = class(TButton)
private
protected
public
published
end;

procedure Register;
implementation

procedure Register;
begin
RegisterComponents ('MyComponents', [TMyBtn]);
end;
end.

At the same time not only the new class TMyBtn on the basis of a standard class of the TButton button is created, but also registration procedure of a new component in a palette of components is described.

3. Describe in the directive private all fields, procedures and functions which are necessary to you for creation of a component, and they will have the status of hidden. Enter a field name (from the letter ""F""), its type. For example, record of a view FDatas: integer describes the FDatas variable of an integral type. List in the section protected event handlers necessary for you, for example from clicking of keys of the keyboard or a mouse. And at inheritance of a class it is necessary to put the keyword override – for overlapping of the parent processor of a default event. So, record procedure Click; override provides interception of mouse click by the button.

4. Functions available to the user and procedures of a component are described in the directives public and published, for example, by means of record of a view: function TSysInfo.GetUser: string or property MachName: string. When using the word property it is possible to specify properties which will be available in an object inspector in the last directive.

5. Write the code of functioning of a component in the announced procedures and functions. Code sample of the processor:
function MachName: string;
var
р: integer;
с: PChar;
begin
с: = stralloc (p);
end;
end.

6. Install a component in the project necessary to you. In the main menu of the Delphi environment select the items Component, Install Component. In the appeared dialog box open one of bookmarks: Into exsisting Package if you want to install a component in the existing package, or Into new Package – in new. Click Ok and confirm request of the application for rewriting of a package (if necessary). Then the written component will be ready to use.

Author: «MirrorInfo» Dream Team


Print