Delphi 5: Building Custom VCL Components
Delphi 5 is the peak of rapid application development on Windows. While Visual Basic developers are struggling with COM and DLL hell, we have the VCL (Visual Component Library). The best part? You can write your own components and install them right into the IDE.
Anatomy of a Component
A VCL component is a Delphi class that usually inherits from TCustomControl or an existing component like TEdit.
unit MyCustomLabel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, StdCtrls;
type
TMyCustomLabel = class(TCustomLabel)
private
FFlashColor: TColor;
procedure SetFlashColor(Value: TColor);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
published
property FlashColor: TColor read FFlashColor write SetFlashColor;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Aunimeda', [TMyCustomLabel]);
end;
constructor TMyCustomLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FFlashColor := clRed;
end;
procedure TMyCustomLabel.Paint;
begin
Canvas.Font.Color := FFlashColor;
inherited Paint;
end;
procedure TMyCustomLabel.SetFlashColor(Value: TColor);
begin
if FFlashColor <> Value then
begin
FFlashColor := Value;
Invalidate; // Force a repaint
end;
end;
end.
The 'published' Section
The magic of Delphi's Object Inspector comes from the published section. Any property listed there is automatically available for editing at design-time via RTTI (Run-Time Type Information).
Delphi's component model is so clean because it's just Object Pascal all the way down. Once you register your component, you can drag and drop it onto a form just like a standard button. This is why we can build Windows apps in hours that take C++ developers weeks.