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

所属分类: 软件编程 / Delphi 阅读数: 117
收藏 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操作图像实现浮雕、反色、模糊、翻转等常用效果的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows API GetLastError()函数返回值含义解释

这篇文章主要介绍了Windows API GetLastError()函数返回值含义解释,本文罗列了所有错误代码及中文注释,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi制作wav文件的方法

这篇文章主要介绍了delphi制作wav文件的方法,涉及Delphi操作多媒体文件的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

ListView 百分比进度条(delphi版)

本文通过实例代码给大家介绍ListView 百分比进度条,本文使用的是delphi语言实现的,代码比较简单实用,希望的朋友参考下
收藏 0 赞 0 分享

Delphi XE5 为Android应用制作签名的方法(图文)

这篇文章主要介绍了Delphi XE5 为Android应用制作签名的方法(图文),需要的朋友可以参考下
收藏 0 赞 0 分享

TImage组件实现保存图片到Stream

这篇文章主要介绍了TImage组件实现保存图片到Stream以及从stream中读取图片的方法,非常的实用,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

Delphi提取PDF文本实例

下面小编就为大家带来一篇Delphi提取PDF文本实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Delphi 中内存映射对于大文件的使用

这篇文章主要介绍了Delphi 中内存映射对于大文件的使用的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi 根据字符串找到函数并执行的实例

这篇文章主要介绍了Delphi 根据字符串找到函数并执行的实例的相关资料,希望通过本能帮助到大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi 用DLL实现插件的简单实例

这篇文章主要介绍了Delphi 用DLL实现插件的简单实例的相关资料,希望通过本文能帮助到大家,这里提供实例帮助大家掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多