Delphi Timer定时器使用


本文整理自网络,侵删。

 

Timer定时器是一个非可视化组件,能够定时触发OnTimer事件,完成模拟时钟、系统延时、倒计时等工作。在System选项卡中。
1. Timer的主要属性
(1) Enabled属性:当值为True时,打开定时器,否则关闭定时器。默认值为true。
(2) Interval属性:控制OnTimer事件触发的时间间隔,单位是毫秒。将Interval设置为0,相当于关闭定时器。默认值为1000ms(1秒)。
2. Timer的主要事件
Timer只有一个OnTimer事件。当Timer打开时,每经过Interval属性指定的时间,Timer就会触发OnTimer事件,执行其中的程序。
例:倒计时
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Spin, ExtCtrls, StdCtrls, Mask, Buttons;
type
TForm1 = class(TForm)
Timer1: TTimer;
MaskEdit1: TMaskEdit;
BitBtn1: TBitBtn;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
m,s:integer;
implementation
{$R *.dfm}
procedure TForm1.Timer1Timer(Sender: TObject);
begin
s:=s-1;
if s=-1 then
begin
s:=59;
m:=m-1;
if m=-1 then m:=59;
end;
MaskEdit1.Text:=inttostr(m)+':'+inttostr(s);
MaskEdit1.SelLength:=0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
m:=9;s:=10;//初始时间09分10秒
maskedit1.EditMask:='!00:00;1;0';//时间格式,未输入字符用0代
timer1.Interval:=1000;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
timer1.Enabled:=not timer1.Enabled;//停止倒计时
end;
end.

相关阅读 >>

Delphi dcc64.exe Delphi64位命令行编译器揭秘

使用Delphi启动和关闭外部应用程序

Delphi 获取\xxx\xxxx\1234最后一级名称

Delphi validatename 过滤特殊字符

Delphi listview1 图标不齐,错乱解决方法

Delphi xe10 针对全面屏手机端无法全面显示,下方显示黑条的处理

Delphi 窗体的位置和高宽度-tform:letf、top、width、height、clientwidth、clientheight

Delphi 农历源码

Delphi 官方使用并行编程库介绍

Delphi获取网卡mac地址的两种方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...