扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:佚名 来源:CSDN 2007年10月31日
关键字:
unit CountUnit; interface uses SysUtils,Classes,Graphics,Controls,ExtCtrls; type //用于设置时间事件发生的间隔 TInterval=(TenM,TwentyM,ThirtyM,FortyM,FiftyM,SixtyM); TTimeCount=class(TCustomPanel) private FTimer:TTimer; FCount:integer; //只读私有成员,计数值 FInterval:TInterval; //时间事件发生的间隔 FActive:Boolean; //决定是否发生间隔事件 TimeLen:Integer;//发生事件的时间长度,以秒为单位。 TimeNum:integer;//计数值,和TimeLen一起有用,以判断是否事件该发生了 FTimeOut:TNotifyEvent;//事件的方法指针 procedure SetInterval(I:TInterval); procedure SetActive(A:boolean); procedure FTimerTimer(Sender:Tobject);//时间控件的事件处理函数 protected procedure DoTimeOut;dynamic; //调度方法,用于关联事件。 public procedure pause; //暂停计数 procedure Resume;//从暂停的计数开始计数。 procedure stop;//停止 procedure start;//开始计数 constructor Create(AOwner:TComponent);override; destructor Destroy;override; property Count:Integer read FCount; //计数值的只读属性 published property Interval:TInterval read FInterval write SetInterval Default TenM; property Active:boolean read FActive write SetActive default false; property OnTimeOut:TNotifyEvent read FTimeOut write FTimeOut; //显式祖先类的一些属性在对象察看器中 property BevelInner; property BevelOuter; property BevelWidth; property Color; property Font; property PopupMenu; property ShowHint; property TabOrder; property TabStop; property Visible; property OnClick; property OnDblClick; property OnMouseDown; property OnMouseMove; property OnMouseUp; end; implementation //构造函数,继承父类的构造函数,并初始化类中的成员。 constructor TTimeCount.Create(AOwner:TComponent); //创建时间控件并设置相关的参数 procedure CreateTimer; begin FTimer:=TTimer.Create(self); FTimer.Enabled:=False; FTimer.Interval:=1000; FTimer.OnTimer:=FTimerTimer; end; //以下是设置外观的 procedure setView; begin Width:=100; Height:=50; Color:=$000000; Font.Color:=$FFFFFF; Font.Size:=14; Font.Style:=[fsBold,fsUnderline];; BevelOuter := bvLowered ; Caption:='0'; end; begin inherited Create(AOwner); FCount:=0; FInterval:=TenM; FActive:=False; TimeLen:=600;//十分钟,六百秒 TimeNum:=0; CreateTimer; setView; end; //析构函数,先释放时间控件,再继承父类的析构函数 destructor TTimeCount.Destroy; begin FTimer.Free; inherited Destroy; end; //设置时间事件发生间隔,财时要赋相应的间隔秒数 procedure TTimeCount.SetInterval(I:TInterval); begin if FInterval<>I then begin FInterval:=I; case FInterval of TenM: TimeLen:=600; TwentyM:TimeLen:=1200; ThirtyM: TimeLen:=1800; FortyM: TimeLen:=2400; FiftyM:TimeLen:=3000; SixtyM:TimeLen:=3600; end; end; end; procedure TTimeCount.SetActive(A:boolean); begin if FActive<>A then begin FActive:=A; TimeNum:=0; end; end; procedure TTimeCount.pause; begin if FTimer.Enabled then FTimer.Enabled:=False; end; procedure TTimeCount.Resume; begin if not FTimer.Enabled then FTimer.Enabled:=True; end; procedure TTimeCount.stop; begin FTimer.Enabled:=False; FCount:=0; TimeNum:=0; caption:='0' end; procedure TTimeCount.start; begin if (not FTimer.Enabled)and(TimeNum=0) then FTimer.Enabled:=True; end; //最重要的时间函数,用于调用该类的事件触发高度函数。 //以及在容器中显示计数值 procedure TTimeCount.FTimerTimer(Sender:TObject); begin inc(FCount); if (FCount mod 3600)=0 then FCount:=0; Caption:=InttoStr(FCount);//这个就是显示计数值 inc(TimeNum); if (TimeNum=TimeLen)and(FActive) then begin DoTimeOut; TimeNum:=0; end; end; //事件调度函数,将外部的事件处理函数和该类的事件方法指针联系起来 procedure TTimeCount.DoTimeOut; begin if Assigned(FTimeOut) then FTimeOut(Self); end; end. |
unit TimeCount; interface uses messages, SysUtils,Classes,Graphics,Controls,ExtCtrls; type TTimeCount=class(TCustomPanel) private published public published end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [TTimeCount]); //注册控件用的函数,自动帮你生成 end; end. |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者