delphi Timer定时器使用  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.032405853271484 seconds