A simple component that looks like 3D light effect diode (LED).
On and off color can be set. OnChange event occurs when on/off state changes.
TdsLed1 (Light effect diode)
Let's start with a new project, save it into desired folder and declare some
vital things for our led.
TdsLed1 = class(TGraphicControl)
procedure SetLedOn(Value: Boolean);
procedure SetOnColor(Value: TColor);
procedure SetOffColor(Value: TColor);
procedure Paint; override;
procedure DoChange; virtual;
constructor Create(AOwner: TComponent); override;
property OnColor: TColor read FOnColor write SetOnColor;
property OffColor: TColor read FOffColor write SetOffColor;
property LedOn: Boolean read FLedOn write SetLedOn;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
TGraphicControl will be ancestor for our TdsLed1. TGraphicControl is the
base class for all nonwindowed custom controls. You can read all about it
in Delphi help.
The reason we picked this one is because we don't need input focus for our
led (that means we don't need a Windows handle). Handles take up system
resources. Since we don't need handle we'll be low on demand to system
resources and painting will be quicker.
We want to be able to set on color and off color. FOnColor and FOffColor
are declared for this.
We want to set on/off state. FLedOn is declared for this.
OnChange event should occur when on/off state changes. FOnChange is declared
We will declare all those things as properties. Also, we will propagate Visible
property from TGraphicControl.
TGraphicControl doesn't do anything by itself. To see something happen we must
override Paint method and draw something onto the canvas.
Now let's write some code.
First of all, we want our constructor to do some initialization. We will override
constructor and write:
FOnColor := clLime;
FOffColor := clRed;
FLedOn := false;
Width := 24;
Height := 24;
As you can see, we defined DoChange as virtual. Only virtual and dynamic methods
can be overridden. We want to allow descant classes to override this method. And
what this method does:
if Assigned(FOnChange) then FOnChange(self);
Nothing much. It only calls FOnChange if it is assigned. And that is all it
Next two methods are simple:
procedure TdsLed1.SetOnColor(Value: TColor);
if FOnColor <> Value then
FOnColor := Value;
procedure TdsLed1.SetOffColor(Value: TColor);
if FOffColor <> Value then
FOffColor := Value;
There is only one thing to discuss here. Refresh. You could call Invalidate,
but you know, that Invalidate would refresh our led, when Windows would have
time to do it. Now let's suppose, you will switch led state at the beginning
of a long process. If we would use invalidate, led would switch to a different
color when Application.ProcessMessages would be called or never, if we wouldn't
call Application.ProcessMessages. Refresh repaints control immediately, before
anything else will go on. Exactly what we need.
When we switch on/off state of a led, this should happen:
procedure TdsLed1.SetLedOn(Value: Boolean);
if Value <> FLedOn then
FLedOn := Value;
If state should change, switch state, refresh led and trigger OnChange event.
Now the big part. Painting.
if FLedOn then
Canvas.Brush.Color := FOnColor
Canvas.Brush.Color := FOffColor;
R := ClientRect;
if Height > Width then
off := Width div 5
off := Height div 5;
with Canvas do
Pen.Color := Canvas.Brush.Color;
Ellipse(R.Left, R.Top, R.Right, R.Bottom);
Pen.Color := clWhite;
Brush.Color := clWhite;
Chord(R.Left+off, R.Top+off, R.Right-off, R.Bottom-off, R.Right div 2, R.Top+off, R.Left+off, R.Bottom div 2);
We defined drawing an ellipse led as a separated procedure. Why? You could
write your own routines to paint rectangle, triangle or whatever. In fact,
we will do that in one of the upcoming articles.
All we do is set up the right color, draw a circle and a 3D effect. I won't
explain Chord method since you should be able to try it out for yourself.
Should we try our code now?
Write OnCreate method of a form:
procedure TForm1.FormCreate(Sender: TObject);
l := TdsLed1.Create(Self);
l.Parent := Self;
Don't forget to declare
in the private section of a TForm1.
Put additional button onto the form and write:
procedure TForm1.Button1Click(Sender: TObject);
l.LedOn := not l.LedOn;