Component and Package Development
45.1 Writing a Custom Component
unit LabeledButton;
interface
uses
System.Classes, Vcl.Controls, Vcl.StdCtrls, Vcl.Graphics;
type
TClickCountEvent = procedure(Sender: TObject; Count: Integer) of object;
TCounterButton = class(TButton)
private
FClickCount: Integer;
FHighlightColor: TColor;
FOnClickCount: TClickCountEvent;
procedure SetHighlightColor(const Value: TColor);
protected
procedure Click; override;
public
constructor Create(AOwner: TComponent); override;
procedure ResetCount;
published
property ClickCount: Integer read FClickCount;
property HighlightColor: TColor read FHighlightColor write SetHighlightColor default clYellow;
property OnClickCount: TClickCountEvent read FOnClickCount write FOnClickCount;
end;
procedure Register;
implementation
constructor TCounterButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHighlightColor := clYellow;
Caption := 'Count: 0';
end;
procedure TCounterButton.Click;
begin
Inc(FClickCount);
Caption := 'Count: ' + FClickCount.ToString;
if Assigned(FOnClickCount) then FOnClickCount(Self, FClickCount);
inherited; // fires OnClick
end;
procedure TCounterButton.ResetCount;
b
…45.2 Packages
Runtime package (.bpl) Contains component code. Can be shared by apps (build with runtime packages) Design-time package
Registers components and editors in the IDE (requires DesignIDE) Runtime + design Combined (simple cases)
Split runtime and design-time packages for professional components. The {$LIBSUFFIX AUTO} directive versions BPL names.
Property editors (TPropertyEditor), component editors (TComponentEditor, context menu verbs), ComponentState (csDesigning, csLoading), Loaded, Notification (for references to other components), DefineProperties (custom streaming), and component icons (.dcr resources).
Custom drawn controls: inherit from TCustomControl / TGraphicControl and override Paint. Handle CMMouseEnter/CMMouseLeave messages and HighDPI scaling (ScaleValue, FCurrentPPI).
FMX components: inherit from TControl/TStyledControl, define style resources, ApplyStyle/FreeStyle.
Plugin systems with packages: LoadPackage/UnloadPackage plus interfaces or RegisterClass/FindClass.