Delphi - Registering Components

When you write code, it is saved in a pas file. To reuse it in another program, you just create a hard link to either the source or the related dcu (Delphi compiled unit) and call the functions or use the contained objects by manually creating them at runtime.

Delphi also provides a Component palette to simplify adding components (reusable code) to forms. Some of the advantages are

To be added to the Component palette, your code needs to be based on TComponent, or one of its descendants. This is the basic hierarchy discussed here.

Base your control on one of the following (or one of their descendants) depending on how the control is displayed (or not) at run time. To add your component to the Component palette, it must be Registered.

Basics | Register the Control | Use a Package


Basics

In Delphi 5, the Component palette (a toolbar with multiple tabs) is at the top of the screen. Using a Delphi package (dpk file) and the Register procedure, you can add icons that represent your components to one of the existing tabs, or you can create new tabs - your choice.

(This is one of the many reasons that I truly hate the new versions of Delphi - the Component palette toolbar was replaced with a list that is very difficult to use with a mouse. Between this and the totally broken help system, I have no problem reporting that the Delphi user interface (post Delphi 7) is user hostile. Add to that the fact that all strings are now unicode, and there is no reason to ever move from Delphi 5 to a less capable product.)

The information used to produce the Component palette is stored in blp files and the registry.

The basic Delphi 5 controls are defined in Explore it using the Resource Hacker (or a similar tool).

When a component is placed on a form


Register the Control

For your control to be added to the Component palette it must be Registered - ie, the source must have a function named Register which calls RegisterComponents. This function must be in one (or more) of the source files included in the package (dpk file). It does not matter if the Register procedure is in the source file that defines the component or not. There are pros and cons to both approaches. The following example uses one pas file to register a component defined in another file. Notice that the "include resource" directive is commented out. Any resources included with that directive will be included in the runtime exe file. (I leave it in, but commented out, as a reminder.) The icon used to represent the component on the toolbar is included in mcSci_Register.dcr as a 24x24 bitmap named TmcTemperature_UIFrame (the name is not case sensitive).

These are the dpk lines that include resources

Note that each source file may also include resources - the same resources if fact. The difference is that the dpk file makes the resources available at design time and the individual source files make them available at design time and runtime.

Each source file may have its own Register procedure. This is the only procedure name that I know of that can be repeated without causing without causing some kind of conflict.


Use a Package

To actually Register the component, the source file (unit) that defines the control and the file that contains the register routine must be included in a package. Before doing anything else, be sure to save (and name) the package.

Once it has a proper name, and the other instructions on creating a package are followed, the package is compiled and installed.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / Delphi / ComponentRegister.html