DBMNG数据库管理与应用

独立思考能力,对于从事科学研究或其他任何工作,都是十分必要的。
当前位置:首页 > 经验分享 > Delphi

DelphiTimer定时器使用

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.
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号