home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2003 February
/
Chip_2003-02_cd1.bin
/
zkuste
/
delphi
/
kompon
/
d4567
/
ADVANTMR.ZIP
/
advantmr.pas
Wrap
Pascal/Delphi Source File
|
2002-11-01
|
4KB
|
158 lines
unit AdvancedTimer;
{
TAdvancedTimer v1.3
Copyright (C) 2002 Johan Stokking (johan@stokking.com)
___________________
}
{$R AdvancedTimer.dcr}
interface
uses
SysUtils, Windows, Classes, Forms, Messages, Consts;
type
TTimerActivateEvent = procedure(Interval: Cardinal) of object;
TTimerDeactivateEvent = procedure(Intervals: Cardinal) of object;
TTimerMaxIntervals = procedure(Intervals: Cardinal) of object;
TAdvancedTimer = class(TComponent)
private
FInterval: Cardinal;
FWindowHandle: HWND;
FOnTimer: TNotifyEvent;
FOnActivate: TTimerActivateEvent;
FOnDeactivate: TTimerDeactivateEvent;
FOnMaxIntervals: TTimerMaxIntervals;
FIntervals: Integer;
FMaxIntervals: Integer;
FStopAtMax: Boolean;
FEnabled: Boolean;
procedure UpdateTimer;
procedure SetEnabled(Value: Boolean);
procedure SetInterval(Value: Cardinal);
procedure SetOnTimer(Value: TNotifyEvent);
procedure WndProc(var Msg: TMessage);
protected
procedure Timer; dynamic;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Enabled: Boolean read FEnabled write SetEnabled default True;
property Interval: Cardinal read FInterval write SetInterval default 1000;
property Intervals: Integer read FIntervals;
property MaxIntervals: Integer read FMaxIntervals write FMaxIntervals default 0;
property StopAtMax: Boolean read FStopAtMax write FStopAtMax default True;
property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer;
property OnActivate: TTimerActivateEvent read FOnActivate write FOnActivate;
property OnDeactivate: TTimerDeactivateEvent read FOnDeactivate write FOnDeactivate;
property OnMaxIntervals: TTimerMaxIntervals read FOnMaxIntervals write FOnMaxIntervals;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('System', [TAdvancedTimer]);
end;
{ TAdvancedTimer }
constructor TAdvancedTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := True;
FInterval := 1000;
FIntervals := 0;
FStopAtMax := True;
{$IFDEF MSWINDOWS}
FWindowHandle := Classes.AllocateHWnd(WndProc);
{$ENDIF}
{$IFDEF LINUX}
FWindowHandle := WinUtils.AllocateHWnd(WndProc);
{$ENDIF}
end;
destructor TAdvancedTimer.Destroy;
begin
FEnabled := False;
UpdateTimer;
{$IFDEF MSWINDOWS}
Classes.DeallocateHWnd(FWindowHandle);
{$ENDIF}
{$IFDEF LINUX}
WinUtils.DeallocateHWnd(FWindowHandle);
{$ENDIF}
inherited Destroy;
end;
procedure TAdvancedTimer.WndProc(var Msg: TMessage);
begin
with Msg do
if Msg = WM_TIMER then
try
Timer;
except
Application.HandleException(Self);
end
else
Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;
procedure TAdvancedTimer.UpdateTimer;
begin
KillTimer(FWindowHandle, 1);
if (FInterval <> 0) and FEnabled then
if SetTimer(FWindowHandle, 1, FInterval, nil) = 0 then
raise EOutOfResources.Create(SNoTimers);
end;
procedure TAdvancedTimer.SetEnabled(Value: Boolean);
begin
if Value <> FEnabled then begin
FEnabled := Value;
UpdateTimer;
if Value and Assigned(FOnActivate) then
OnActivate(FInterval)
else if not Value and Assigned(FOnDeactivate) then
OnDeactivate(FIntervals);
FIntervals := 0;
end;
end;
procedure TAdvancedTimer.SetInterval(Value: Cardinal);
begin
if Value <> FInterval then
begin
FInterval := Value;
UpdateTimer;
end;
end;
procedure TAdvancedTimer.SetOnTimer(Value: TNotifyEvent);
begin
FOnTimer := Value;
UpdateTimer;
end;
procedure TAdvancedTimer.Timer;
begin
if Assigned(FOnTimer) then
FOnTimer(Self);
FIntervals := FIntervals +1;
if FIntervals = FMaxIntervals then begin
OnMaxIntervals(FIntervals);
if FStopAtMax then
SetEnabled(False);
end;
end;
end.