Delphi完成毫秒級別的倒計時實例代碼。本站提示廣大學習愛好者:(Delphi完成毫秒級別的倒計時實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Delphi完成毫秒級別的倒計時實例代碼正文
本文以實例簡述了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.