手机
当前位置:查字典教程网 >编程开发 >Delphi >Delphi实现毫秒级别的倒计时实例代码
Delphi实现毫秒级别的倒计时实例代码
摘要:本文以实例简述了Delphi实现毫秒级别倒计时的方法。一般来说可以获得系统的高性能频率计数器在一毫秒内的震动次数,如果时钟震动次数超过10毫...

本文以实例简述了Delphi实现毫秒级别倒计时的方法。一般来说可以获得系统的高性能频率计数器在一毫秒内的震动次数,如果时钟震动次数超过10毫秒的次数则刷新edit3的显示,显示从开始记数到记数实际经过的时间,具体实现代码如下:

unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, mmsystem; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button1: TButton; Button2: TButton; Timer1: TTimer; Label1: TLabel; Label2: TLabel; Label3: TLabel; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; acttime1,acttime2:cardinal; smmcount,stimercount,spcount:single; htimeid:integer; iten:integer; protimecallback:tfntimecallback; procedure timeproc(utimerid, umessage: uint; dwuser, dw1, dw2: dword) stdcall; procedure proendcount; implementation {$R *.DFM} //timesetevent的回调函数 procedure proendcount; begin acttime2:=gettickcount-acttime1; form1.button2.enabled :=false; form1.button1.enabled :=true; form1.timer1.enabled :=false; smmcount:=60; stimercount:=60; spcount:=-1; timekillevent(htimeid); end; procedure timeproc(utimerid, umessage: uint; dwuser, dw1, dw2: dword) stdcall; begin form1.edit2.text:=floattostr(smmcount); smmcount:=smmcount-0.01; end; procedure TForm1.FormCreate(Sender: TObject); begin button1.caption :='开始倒计时'; button2.caption :='结束倒计时'; button2.enabled :=false; button1.enabled :=true; timer1.enabled :=false; smmcount:=60; stimercount:=60; spcount:=60; end; procedure TForm1.Button1Click(Sender: TObject); var lgtick1,lgtick2,lgper:tlargeinteger; ftemp:single; begin button2.enabled :=true; button1.enabled :=false; timer1.enabled :=true; timer1.interval :=10; protimecallback:=timeproc; htimeid:=timesetevent(10,0,protimecallback,1,1); acttime1:=gettickcount; //获得系统的高性能频率计数器在一毫秒内的震动次数 queryperformancefrequency(lgper); ftemp:=lgper/1000; iten:=trunc(ftemp*10); queryperformancecounter(lgtick1); lgtick2:=lgtick1; spcount:=60; while spcount>0 do begin queryperformancecounter(lgtick2); //如果时钟震动次数超过10毫秒的次数则刷新edit3的显示 if lgtick2 - lgtick1 > iten then begin lgtick1 := lgtick2; spcount := spcount - 0.01; edit3.text := floattostr(spcount); application.processmessages; end; end; end; procedure TForm1.Timer1Timer(Sender: TObject); begin edit1.text := floattostr(stimercount); stimercount:=stimercount-0.01; end; procedure TForm1.Button2Click(Sender: TObject); begin proendcount; //显示从开始记数到记数实际经过的时间 showmessage('实际经过时间'+inttostr(acttime2)+'毫秒'); end; end.

【Delphi实现毫秒级别的倒计时实例代码】相关文章:

delphi 正弦曲线图

Delphi实现获取磁盘空间大小的方法

Delphi隐藏TPageControl的标签实例介绍

Delphi用TActionList实现下载文件的方法

Delphi实现窗体感知鼠标滑过并自动隐藏与显示窗口的方法

delphi字符串分隔函数用法实例

Delphi 生成excel中饼图的实例代码

delphi实现保存和读取图片的方法

Delphi实现窗口文字淡入淡出渐变效果的方法

Delphi解析FTP地址的方法

精品推荐
分类导航