This article describes the method of Delphi implementation of millisecond-level countdown with examples. In general, you can get the system's high-performance frequency counter in a millisecond of the number of vibrations, if the clock vibration times more than 10 milliseconds to refresh the display of edit3, showing from the beginning of the count to the actual elapsed time , the specific implementation code is as follows:
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 callback Function 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: = ' Start countdown ';
Button2.caption: = ' End Countdown ';
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;
Obtain the system's high-performance frequency counter in a millisecond of the vibration times QueryPerformanceFrequency (lgper);
ftemp:=lgper/1000;
Iten:=trunc (FTEMP*10);
QueryPerformanceCounter (LGTICK1);
Lgtick2:=lgtick1;
spcount:=60; While SPCOUNT>0 do begin QueryPerformanceCounter (LGTICK2);
If the clock vibrates more than 10 milliseconds, refresh the edit3 display 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;
Shows the actual elapsed time showmessage (' actual elapsed time ' +inttostr (acttime2) + ' milliseconds ') from the start count to the count;
End End.