Delphi實現(xiàn)毫秒級別的倒計時實例代碼
本文以實例簡述了Delphi實現(xiàn)毫秒級別倒計時的方法。一般來說可以獲得系統(tǒng)的高性能頻率計數(shù)器在一毫秒內的震動次數(shù),如果時鐘震動次數(shù)超過10毫秒的次數(shù)則刷新edit3的顯示,顯示從開始記數(shù)到記數(shù)實際經(jīng)過的時間,具體實現(xiàn)代碼如下:
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的回調函數(shù)
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;
//獲得系統(tǒng)的高性能頻率計數(shù)器在一毫秒內的震動次數(shù)
queryperformancefrequency(lgper);
ftemp:=lgper/1000;
iten:=trunc(ftemp*10);
queryperformancecounter(lgtick1);
lgtick2:=lgtick1;
spcount:=60;
while spcount>0 do
begin
queryperformancecounter(lgtick2);
//如果時鐘震動次數(shù)超過10毫秒的次數(shù)則刷新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;
//顯示從開始記數(shù)到記數(shù)實際經(jīng)過的時間
showmessage('實際經(jīng)過時間'+inttostr(acttime2)+'毫秒');
end;
end.
上一篇:Delphi實現(xiàn)窗口文字淡入淡出漸變效果的方法
欄 目:Delphi
本文標題:Delphi實現(xiàn)毫秒級別的倒計時實例代碼
本文地址:http://www.jygsgssxh.com/a1/Delphi/8642.html
您可能感興趣的文章
- 01-10在Delphi實現(xiàn)在數(shù)據(jù)庫中存取圖像的圖文演示無錯
- 01-10delphi建立、讀取、存貯INI文件的方法《三》
- 01-10Delphi Command模式
- 01-10delphi 正弦曲線圖
- 01-10delphi建立、讀取、存貯INI文件的方法《二》
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函數(shù)
- 01-10delphi中一個值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程


閱讀排行
本欄相關
- 01-10在Delphi實現(xiàn)在數(shù)據(jù)庫中存取圖像的圖
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10delphi 正弦曲線圖
- 01-10Delphi Command模式
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函
- 01-10delphi中一個值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


