Delphi实现毫秒级别的倒计时实例代码

所属分类: 软件编程 / Delphi 阅读数: 80
收藏 0 赞 0 分享

本文以实例简述了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是过了最辉煌的时代。但为什么要继续下去,而不转向其它的?这是不是死脑筋
收藏 0 赞 0 分享

Delphi 调用外部程序并阻塞到外部程序中

这篇文章主要介绍了Delphi 调用外部程序并阻塞到外部程序中的相关知识,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

修改 Delphi 10.3.3 IDE 字体和字体大小

Delphi,是Windows平台下著名的快速应用程序开发工具(Rapid Application Development,简称RAD)。这篇文章主要介绍了修改 Delphi 10.3.3 IDE 字体和字体大小,需要的朋友可以参考下
收藏 0 赞 0 分享

解决delphi TAdoQuery组件的close方法导致”列名无效“错误的问题

今天小编就为大家分享一篇解决delphi TAdoQuery组件的close方法导致”列名无效“错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

delphi 字符串处理中的怪异现象与处理方式

今天小编就为大家分享一篇delphi 字符串处理中的怪异现象与处理方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

完美解决Indy 编译提示版本不一致的问题

今天小编就为大家分享一篇完美解决Indy 编译提示版本不一致的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

delphi 正弦曲线图

 在Delphi中没有直接正弦曲线的函数。但是在科学研究中,正弦曲线的应用是非常广泛的,本实例介绍如何绘制正弦曲线。
收藏 0 赞 0 分享

Delphi中判断文件是否为文本文件的函数

在用Delphi 编程处理文本文件的时候,如果要装载的文件比较大,你可能会想在文件装载过程中提供一个装载进度的提示--那么你就必需自己编写直接通过文件打开/读取函数来装载文件内容的函数。
收藏 0 赞 0 分享

c# 多线程编程 入门篇

多线程应该是编程工作者的基础技能, 但这个基础对我来讲的确有点难(起码昨天以前是这样).
收藏 0 赞 0 分享

DELPHI7.0 获取硬盘、CPU、网卡序列号的代码

DELPHI7.0 获取硬盘、CPU、网卡序列号的代码,使用DELPHI编程的朋友可以参考下。
收藏 0 赞 0 分享
查看更多