(接上文)
組件的代碼由於假設你已經熟悉delphi開發(它和一般開發沒什麼不同),我們就直接貼出來並加上適當的注釋:
unit Clock;
interface
uses
SysUtils, Classes, Controls, StdCtrls,ExtCtrls;
type
TState=(StClock,StRunClock,StBackClock);//定義枚舉類表示控件的3種狀態:時鐘、跑表、倒計時鐘
TClock = class(TCustomLabel)
private
fState:TState;
fTimer:TTimer;//為什麼使用這個組件作為我們組件的私有成員就不用說了吧
RCD:array[1..8] of integer;//跑表中的各個數位。
fBeginTime:string;//到計時時的開始時鐘,之所以沒用TTime類型是為了在後面演示屬性編輯器
fWakeTime:string;//鬧鐘時間,出於和上面同樣的理由
fAllowWake:boolean;//是否開啟鬧鐘功能
fOnWakeUp:TNotifyEvent;//為了使組件更加完美,我們允許組件用戶能夠響應鬧鐘到來時的時件
fOnTimeUp:TNotifyEvent;//同上能夠響應倒計時種完成時的事件,我們將發布這兩個事件
function GetActive:boolean;//控制Timer是否工作以控制3種狀態的鐘是否工作
procedure SetActive(Value:boolean);
procedure SetState(Value:TState);
procedure SetBeginTime(Value:string);
procedure SetWakeTime(Value:string);
protected
procedure WalkClock(sender:TObject);//作為時鐘時走種的事件
procedure RunClock(sender:TObject); //跑表
procedure BackClock(sender:TObject);//倒計時
public
constructor Create(AOwner:TComponent);override;//完成一些初始化工作
procedure ReSetRunClock; //跑表和倒計時都需要一個復位方法給組件使用者調用
procedure ReSetBackClock;
published
property State:TState read fState write SetState default StClock;//默認為時鐘狀態
property Active:boolean read GetActive write SetActive;//控制3種狀態的鐘是否工作
property BeginTime:string read fBeginTime write SetBeginTime;
property WakeTime:string read fWakeTime write SetWakeTime;
property AllowWake:boolean read fAllowWake write fAllowWake;
property OnWakeUp:TNotifyEvent read fOnWakeUp write fOnWakeUp;
property OnTimeUp:TNotifyEvent read fOnTimeUp write fOnTimeUp;
//最後我們再發布一些被TCustomLabel所隱藏而我們又需要的屬性
property Align;
property Alignment;
property Color;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
property Transparent;
property OnClick;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents(ClockAndTime, [TClock]);
end;
{ TClock }
constructor TClock.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//設置默認值
fTimer:=TTimer.Create(self);
//將它屬於我們的組件,這樣便不用編寫析構函數,而可以自動在釋放本組件時釋放Timer
Active:=false;
AllowWake:=false;
State:=StClock;
BeginTime:=00:00:00;
WakeTime:=00:00:00;
end;
function TClock.GetActive: boolean;
begin
result:=fTimer.Enabled;
end;
procedure TClock.SetActive(Value: boolean);
begin
fTimer.Enabled:=Value;
end;
procedure TClock.SetState(Value: TState);
var
i:integer;
begin
case Value of
StClock:
begin
Active:=false;
fTimer.Interval:=1000;
fTimer.OnTimer:=WalkClock;
Active:=true;
end;
StRunClock://由於Time類型不好處理微秒操作,我們只有手工模仿這個操作,代碼會稍微煩瑣
begin
Active:=false;
for i:=1 to 8 do RCD[i]:=0;
Caption:=IntToStr(RCD[8])+IntToStr(RCD[7])+:+IntToStr(RCD[6])+IntToStr(RCD[5])+:+IntToStr(RCD[4]);
Caption:=Caption+IntToStr(RCD[3])+:+IntToStr(RCD[2])+IntToStr(RCD[1]);
fTimer.Interval:=10;
//經過測試,這個秒表的效果很好,然而這只是一個技術上的演示,
//實際上這麼頻繁(1/100秒)的不斷執行RunClock會使CPU的占用一直達到100%
//這並不是一個好注意。事實上要想在跑表中顯示微秒級別並做到合理的占用CPU
//這需要更加靈活和復雜的編程
fTimer.OnTimer:=RunClock;
end;
StBackClock:
begin
Active:=false;
Caption:=BeginTime;
fTimer.Interval:=1000;
fTimer.OnTimer:=BackClock;
end;
end;
fState:=Value;
end;
procedure TClock.SetBeginTime(Value: string);
begin
try
StrToTime(Value);
fBeginTime:=Value;
if State=StBackClock then
begin
Active:=false;
Caption:=Value;
end;
except
on Exception do
begin
fBeginTime:=00:00:00;
if State=StBackClock then Caption:=00:00:00;
end;
end;
end;
procedure TClock.SetWakeTime(Value: string);
begin
try
StrToTime(Value);
fWakeTime:=Value;
except
on Exception do
begin
fWakeTime:=00:00:00;
end;
end;
end;
procedure TClock.WalkClock(sender: TObject);
begin
Caption:=TimeToStr(Time);
if AllowWake and (StrToTime(Caption)=StrToTime(WakeTime)) then
begin
Beep;//蜂鳴器
if Assigned(fOnWakeUp) then
fOnWakeUp(self);
end;
end;
procedure TClock.RunClock(sender: TObject);
begin
RCD[1]:=RCD[1]+1;
if RCD[1]=10 then begin RCD[2]:=RCD[2]+1;RCD[1]:=0; end;
if RCD[2]=10 then begin RCD[3]:=RCD[3]+1;RCD[2]:=0; end;
if RCD[3]=10 then begin RCD[4]:=RCD[4]+1;RCD[3]:=0; end;
if RCD[4]=6 then begin RCD[5]:=RCD[5]+1;RCD[4]:=0; end;
if RCD[5