Creating a New Component


In order to use the new component, you have to add it to a package. A package is a special dynamic link library with a .bpl (Borland Package Library) extension that can be used by the IDE at design time and by applications at run time.

There are two types of packages:

  • Design-time packages — contain components and property editors that are used by the IDE at design time

  • Run-time packages — contain functionality that is used at run time

Packages can also be both run-time and design-time. These run-time/design- time packages usually contain components that have the minimum amount of design-time specific code. This type of package is automatically created by the IDE, as you can see in Figure 24-1.

image from book
Figure 24-1: A run-time/design-time package

So, before creating a new component, you have to either open an existing package or create a new one. To create a new package, select File ® New ®Package - Delphi for Win32. If you have created a new package, you should save it before going any further.

Now that you have a package opened in the IDE, you can create a new component by selecting Component ® New VCL Component. The New VCL Component dialog box that appears first asks you to select the ancestor class of your new component. The first component that we are going to build will be a simple nonvisual component, so select the TComponent class from the list and then press Next (see Figure 24-2).

image from book
Figure 24-2: Selecting the ancestor class

Now that you have selected the ancestor class, you have to name the class, select or type the name of the Tool Palette category in which to place the component, and click the (…) button next to the Unit name text box to define the file name of the component's unit. You should now create a component called TSimple, place it in the "My Components" category (you can, of course, type anything you want here), and save it in the package's directory under the name image from book Simple.pas (see Figure 24-3). Make sure you don't forget the .pas extension in the unit's file name.

image from book
Figure 24-3: Defining the component's class name

In the final step, the New VCL Component dialog box enables you to create the unit and add it to an existing package. Select the Add unit to PackageName.bdsproj project button and click Finish to create the TSimple component.

image from book
Figure 24-4: Adding the new component to the opened package

Listing 24-1 shows the source code generated by the New VCL Component dialog box.

Listing 24-1: The source code of the TSimple component

image from book
unit Simple; interface uses   SysUtils, Classes; type   TSimple = class(TComponent)   private     { Private declarations }   protected     { Protected declarations }   public     { Public declarations }   published     { Published declarations }   end; procedure Register; implementation procedure Register; begin   RegisterComponents('My Components', [TSimple]); end; end.
image from book

The only thing not seen before is the Register procedure. The Register procedure is the only case-sensitive procedure in Delphi and it has only one purpose: to register one or a set of components in the IDE. To register the components, the Register procedure calls the RegisterComponents procedure that is able to register one or more components in the IDE, as long as they all appear in the same Tool Palette category. Here's an example of how you can register a set of components using the RegisterComponents procedure:

procedure Register; begin   RegisterComponents('My Components', [TSimple, TSecondComponent]); end;

The last thing that we have to do in order to add the new component to the Tool Palette is install the package. To install the entire package, right-click the package icon in the Project Manager and select Install. A message box will appear that will notify you if the package was successfully installed or not.

image from book
Figure 24-5: Successful installation of the package

If the package was successfully installed, all the components passed to the RegisterComponents procedure will be registered in the IDE.

To test the new component, you can add a new VCL Forms application project to the project group (right-click the ProjectGroup1 icon in the Project Manager and select Add New Project) and then use your component as you would any other.

image from book
Figure 24-6: The TSimple component at design time



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net