How To Write A Component

Table of contents:

How To Write A Component
How To Write A Component

Video: How To Write A Component

Video: How To Write A Component
Video: Creating a component library with raw CSS 2024, December
Anonim

Delphi's open programming environment is based on the use of various components. A component is code that performs a specific task using properties, events, and procedures. When creating a component, you need to set the values of variables and implement the code of event handlers. To be used in the program, the new component must be included in the project package.

How to write a component
How to write a component

It is necessary

Delphi development environment

Instructions

Step 1

Select the type of component to create. It can be a Windows item, graphic item, control object, or non-visual component. Also, your object can inherit from any existing class. Decide on the functions that you will assign the component to implement.

Step 2

Start the Delphi development environment. In the main application menu, open the Component, New Component items. In the dialog box that appears, in the Ancestor Type field, select the component class that you want to modify. If you are not using inheritance, in the Class Name field, simply enter a name for the new component that starts with the letter "T". In the Palette Page field, write the name of the component tab after installation, then click the Create Unit button. The development environment will automatically generate a template for the new component. An example of the generated Pascal code:

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 a new class TMyBtn was formed based on the standard class of the TButton button, but also the procedure for registering a new component in the component palette is described.

Step 3

In the private directive, describe all the fields, procedures and functions that you need to create the component, and they will have the status of hidden. Specify the field name (with the letter "F"), its type. For example, a record of the form FDatas: integer describes a variable FDatas of an integer type. In the protected section, list the event handlers that you need, for example, from keyboard or mouse key presses. Moreover, when inheriting a class, you must set the override keyword - to overlap the parent handler of the standard event. For example, the entry procedure Click; override ensures that the mouse click on the button is intercepted.

Step 4

The functions and procedures of the component available to the user are described in the public and published directives, for example, using a record of the form: function TSysInfo. GetUser: string or property MachName: string. In the last directive, when using the word property, you can specify properties that will be available in the object inspector.

Step 5

Write the actual code for the functioning of the component in the declared procedures and functions. Sample handler code:

function MachName: string;

var

p: integer;

with: PChar;

begin

c: = stralloc (p);

end;

end.

Step 6

Install the component into the project you need. From the Delphi main menu, select Component, Install Component. In the dialog box that appears, open one of the tabs: Into exsisting Package, if you want to install the component into an existing package, or Into new Package - into a new one. Click OK and confirm the application's request to overwrite the package (if necessary). After that, the written component will be ready for use.

Recommended: