unit compFastTimer;
interface
uses
Windows, SysUtils, MMSystem, Classes;
type
TFastTimer =
class(TComponent)
private
fTimerID: Longword;
fInterval: Integer;
pSelfData : PMMTimerData;
FOnTimer: TNotifyEvent;
fEnabled: Boolean;
procedure SetInterval(Value: integer);
function GetInterval: integer;
procedure SetEnabled(
const Value: Boolean);
public
constructor Create(Owner: TComponent);
Override;
destructor Destroy;
override;
published
property Enabled: Boolean
read fEnabled
write SetEnabled;
property Interval: integer
read GetInterval
write SetInterval;
property OnTimer: TNotifyEvent
read FOnTimer
write FOnTimer;
end;
implementation
Const
TIME_KILL_SYNCHRONOUS = $0100;
procedure TimerCallback(uTimerID, uMessage: UINT; dwUser, dw1, dw2: DWORD)
stdcall;
pascal;
var
ATimer: TFastTimer;
begin
if dwUser <> 0
then
begin
ATimer := TFastTimer(dwUser);
if Assigned(ATimer.fOnTimer)
then
ATimer.fOnTimer(ATimer);
end;
end;
{ TFastTimer }
constructor TFastTimer.Create(Owner: TComponent);
begin
inherited;
fTimerID := 0;
fInterval := 1000;
end;
destructor TFastTimer.Destroy;
begin
Enabled := FALSE;
inherited;
end;
function TFastTimer.GetInterval: integer;
begin
Result := fInterval;
end;
procedure TFastTimer.SetInterval(Value: integer);
begin
fInterval := Value;
end;
procedure TFastTimer.SetEnabled(
const Value: Boolean);
begin
if fEnabled <> Value
then
begin
fEnabled := Value;
if fEnabled
then
begin
// Timer starten ...
fTimerID := timeSetEvent(fInterval, 0, @TimerCallback, Integer(Self), TIME_PERIODIC
Or TIME_KILL_SYNCHRONOUS);
end
else
begin
timeKillEvent(fTimerID);
fTimerID := 0;
end;
end;
end;
end.