现在的位置: 首页 > 综合 > 正文

倒计时对话框

2013年12月13日 ⁄ 综合 ⁄ 共 1463字 ⁄ 字号 评论关闭
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure DialogTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  AMsgDialog: TForm;
  AProgressBar: TProgressBar;
implementation

{$R *.dfm}

procedure TForm1.DialogTimer(Sender: TObject);
var
  aPB: TProgressBar;
begin
  if not (Sender is TTimer) then Exit;
  if ((Sender as TTimer).Owner) is TForm then
    with ((Sender as TTimer).Owner) as TForm do
    begin
      aPB := TProgressBar(FindComponent('Progress'));
      AMsgDialog.Caption := 'You have '+inttostr(10-AProgressBar.Position)+' seconds';
      if aPB.Position >= aPB.Max then
        ModalResult := mrNo
      else
        aPB.StepIt;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ATimer: TTimer;
begin
  AMsgDialog := CreateMessageDialog('Quickly! Answer Yes or No!', mtWarning, [mbYes, mbNo]);
  AProgressBar := TProgressBar.Create(AMsgDialog);
  ATimer := TTimer.Create(AMsgDialog);
  with AMsgDialog do
  try
    Tag := 10; //seconds!
   // Caption := 'You have '+inttostr(AProgressBar.Position)+' seconds';
    Height := 150;
    with AProgressBar do
    begin
      Name := 'Progress';
      Parent := AMsgDialog;
      Max := AMsgDialog.Tag; //seconds
      Step := 1;
      Top := 95;
      Left := 8;
      Width := AMsgDialog.ClientWidth - 16;
    end;
    with ATimer do
    begin
      Interval := 1000;
      OnTimer := DialogTimer;
    end;

    case ShowModal of
      ID_YES: ShowMessage('Answered "Yes".');
      ID_NO: ShowMessage('Answered "No".');
      ID_CANCEL: ShowMessage('Time up!')
    end; //case
  finally
    ATimer.OnTimer := nil;
    Free;
  end;
end;
end.

抱歉!评论已关闭.