A Clock Component for Delphi

by Jim Karabatsos, GUI Computing

One of the things that I seem to do over and over again is set up an on-screen clock display on the main form status line of my applications.

Now there is nothing hard about displaying the time on a form. Like VB, Delphi has a Timer component (of type TTimer) that can be used to generate periodic events. These events are controlled by both an Enabled property (which defaults to True) and an Interval property which specifies the number of milliseconds between events (default 1000). Of course, both VB and Delphi Timers are simply wrappers around a Windows timer, so they are in fact limited to a resolution no better than about 55 milliseconds.

Now in Delphi, you could do it exactly the same way as in VB - go through the whole rigmarole of creating a Label, setting its caption, etc, etc - or, because Delphi promotes the use of components as a means for re-using code, you could create a special type of label control that can display the time all by itself. Once done, you simply choose this new label type from the toolbox (or Component Palette in Delphi-speak) and place it where you want it on the form. Thatís it.

Image of 'How To' title The listing is the complete source code to just such a component.

Thatís about all there is to this control. You should also create a bitmap to be used on the component palette. Using the image editor, create a new .DCR file and give it the same root name as the .PAS file that contains the component. It should be stored in the same directory. Inside that DCR, create a 24 by 24 pixel bitmap and give it the same name as the component in upper case (in this case, TCLOCK).

Written by: Jim Karabatsos
August 95

Image of arrow to next article