明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

通過案例看VCL組件開發(fā)全過程(二)

[摘要](接上文)組件的代碼由于假設(shè)你已經(jīng)熟悉delphi開發(fā)(它和一般開發(fā)沒什么不同),我們就直接貼出來并加上適當(dāng)?shù)淖⑨專簎nit Clock;interfaceuses SysUtils, Classes, Controls, StdCtrls,ExtCtrls;type TState=(StCl...
(接上文)

組件的代碼由于假設(shè)你已經(jīng)熟悉delphi開發(fā)(它和一般開發(fā)沒什么不同),我們就直接貼出來并加上適當(dāng)?shù)淖⑨專?br>


unit Clock;



interface



uses

  SysUtils, Classes, Controls, StdCtrls,ExtCtrls;



type

  TState=(StClock,StRunClock,StBackClock);//定義枚舉類表示控件的3種狀態(tài):時鐘、跑表、倒計時鐘



  TClock = class(TCustomLabel)

  private

    fState:TState;

    fTimer:TTimer;//為什么使用這個組件作為我們組件的私有成員就不用說了吧

    RCD:array[1..8] of integer;//跑表中的各個數(shù)位。

    fBeginTime:string;//到計時時的開始時鐘,之所以沒用TTime類型是為了在后面演示屬性編輯器

    fWakeTime:string;//鬧鐘時間,出于和上面同樣的理由

    fAllowWake:boolean;//是否開啟鬧鐘功能

    fOnWakeUp:TNotifyEvent;//為了使組件更加完美,我們允許組件用戶能夠響應(yīng)鬧鐘到來時的時件

    fOnTimeUp:TNotifyEvent;//同上能夠響應(yīng)倒計時種完成時的事件,我們將發(fā)布這兩個事件

    function GetActive:boolean;//控制Timer是否工作以控制3種狀態(tài)的鐘是否工作

    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; //跑表和倒計時都需要一個復(fù)位方法給組件使用者調(diào)用

    procedure ReSetBackClock;

  published

    property State:TState read fState write SetState default StClock;//默認(rèn)為時鐘狀態(tài)

    property Active:boolean read GetActive write SetActive;//控制3種狀態(tài)的鐘是否工作

    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;

    //最后我們再發(fā)布一些被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);

  //設(shè)置默認(rèn)值

  fTimer:=TTimer.Create(self);

  //將它屬于我們的組件,這樣便不用編寫析構(gòu)函數(shù),而可以自動在釋放本組件時釋放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;

    //經(jīng)過測試,這個秒表的效果很好,然而這只是一個技術(shù)上的演示,

    //實際上這么頻繁(1/100秒)的不斷執(zhí)行RunClock會使CPU的占用一直達到100%

    //這并不是一個好注意。事實上要想在跑表中顯示微秒級別并做到合理的占用CPU

    //這需要更加靈活和復(fù)雜的編程

    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]=10 then begin RCD[6]:=RCD[6]+1;RCD[5]:=0; end;

if RCD[6]=6 then begin RCD[7]:=RCD[7]+1;RCD[6]:=0; end;

if RCD[7]=10 then begin RCD[8]:=RCD[8]+1;RCD[7]:=0; end;

if RCD[8]=10 then RCD[8]:=0; //我們的跑表最多可計99個小時;

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]);

end;



procedure TClock.BackClock(sender: TObject);//可以在一天之類的時間倒計時

begin

if StrToTime(Caption)<>StrToTime('00:00:00') then

  Caption:=TimeToStr(StrToTime(Caption)-0.00001)

else

begin

  Active:=false;

  Beep;

  if Assigned(fOnTimeUp) then

   fOnTimeUp(self);

end;

end;



procedure TClock.ReSetBackClock;

var

i:integer;

begin

if State=StRunClock then

begin

  Active:=false;

  for i:=1 to 8 do RCD[i]:=0;

  Caption:='00:00:00:00';

end;

end;



procedure TClock.ReSetRunClock;

begin

if State=StBackClock then

begin

  Active:=false;

  Caption:=BeginTime;

end;

end;



end.



為了測試我們的組件,現(xiàn)在你就可以安裝這個組件包并建立一個應(yīng)用測試它了,點擊組件包窗體中的install即可(注意:一但你安裝了組件包,當(dāng)你想對組件修改時,在修改了原代碼以后只用點擊組件窗體的compile就可以了更新組件了),這時delphi的組件頁的最后多出了我們定義的頁,其中有了我們的組件!

然而這個組件到目前為止仍然不夠完善,還不能正式發(fā)布給用戶,在下一篇中我們將解決兩個重要的問題:1、給我們的組件添加一個默認(rèn)的圖標(biāo)。2、將這個組件雜亂的屬性歸類。