unit classe.blinklabel;
(*
Usage:
var
BlinkLabel: TBlinkLabel;
begin
BlinkLabel := TBlinkLabel.Create(Form1.Label1, 5); // Label1 will blink for 5 times
BlinkLabel.Start := True;
*)
{$mode objfpc}{$H+}
interface
uses
Classes,
Forms,
SysUtils,
ExtCtrls,
StdCtrls,
Graphics;
type
TBlinkLabel = class(TObject)
private
FCountLimit: Integer;
FMyLabel: TLabel;
FStart: Boolean;
FMyTimer: TTimer;
FCurrentCount: Cardinal;
FOldFontStyles: TFontStyles;
FOldColor: TColor;
FOldMsg: String;
procedure SetStart(AValue: Boolean);
procedure OnTimer(Sender: TObject);
public
constructor Create(ALabel: TLabel; ACountLimit: Cardinal);
destructor Destroy; override;
property CountLimit: Integer read FCountLimit write FCountLimit;
property MyLabel: TLabel read FMyLabel write FMyLabel;
property Start: Boolean read FStart write SetStart;
end;
implementation
constructor TBlinkLabel.Create(ALabel: TLabel; ACountLimit: Cardinal);
begin
inherited Create;
FMyLabel := ALabel;
FCountLimit := ACountLimit;
FCurrentCount := 0;
FOldFontStyles := ALabel.Font.Style;
FOldColor := ALabel.Color;
FOldMsg := ALabel.Caption;
// Creates and configures the Timer
FMyTimer := TTimer.Create(nil);
FMyTimer.Interval := 2000; // 2-second interval
FMyTimer.OnTimer := @OnTimer;
FMyTimer.Enabled := False;
end;
destructor TBlinkLabel.Destroy;
begin
FMyTimer.Free;
inherited Destroy;
end;
procedure TBlinkLabel.SetStart(AValue: Boolean);
begin
if FStart = AValue then Exit;
FStart := AValue;
if FStart then
begin
FCurrentCount := 0;
FMyTimer.Enabled := True;
end
else
begin
FMyTimer.Enabled := False;
// Returns to the initial state
FMyLabel.Font.Style := FOldFontStyles;
FMyLabel.Color := FOldColor;
FMyLabel.Caption := FOldMsg;
Self.Free; // Class self-destruction
end;
end;
procedure TBlinkLabel.OnTimer(Sender: TObject);
begin
FMyTimer.Enabled := False;
Inc(FCurrentCount);
// Toggles the font style between normal and bold
if FMyLabel.Caption = '' then
FMyLabel.Caption := FOldMsg
else
FMyLabel.Caption := '';
FMyLabel.Refresh;
Beep; // Debug purposes, if it reaches here, you will hear a beep
// Checks if the specified seconds have passed
if FCurrentCount >= FCountLimit then
begin
FMyLabel.Font.Style := FOldFontStyles; // Restores the font to the initial state
FMyLabel.Color := FOldColor;
FMyLabel.Caption := FOldMsg;
Self.Free; // Class self-destruction
end
else
begin
Application.ProcessMessages;
FMyTimer.Enabled := True;
end;
end;
end.