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.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值