当前位置:编程学习 > Delphi >>

通过实例看VCL组件开发全过程(二)

(接上文)

组件的代码由于假设你已经熟悉delphi开发(它和一般开发没什么不同),我们就直接贴出来并加上适当的注释:



unit Clock;



inte易做图ce



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
补充:软件开发 , Delphi ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,