delphi 倒计时对话框


本文整理自网络,侵删。

 


unit CountDownCtrl platform;

{$R-,T-,H+,X+}

interface

uses Windows, Messages, SysUtils, Classes, Controls, Graphics, Forms,
  Menus, StdCtrls, Buttons, ExtCtrls;

function MyMessageDlg(var MsgStr: string; Interval: Longint): Boolean;

implementation

uses Consts, Dialogs;

type

  TCountDownDlg = class(TForm)
    MessageLbl: TLabel;
    CountDownTmr: TTimer;
    OKButton: TButton;
    procedure FormCreate(Sender: TObject);
    procedure OKClick(Sender: TObject);
    procedure OnCountDownTmr(Sender: TObject);
  private
    { Private declarations }
    Count: Integer;
    FPrompt: Boolean;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    property Prompt: Boolean read FPrompt write FPrompt default False;
  end;

{ TSelectDirDlg }

constructor TCountDownDlg.Create(AOwner: TComponent);
begin
  inherited CreateNew(AOwner);
  Caption := '倒计时!';
  BorderIcons := [];
  BorderStyle := bsDialog;
  ClientWidth := 424;
  ClientHeight := 255;
  Position := poScreenCenter;
  Font.Name:='宋体';
  Font.Size:=12;
  MessageLbl := TLabel.Create(Self);
  with MessageLbl do
  begin
    Parent := Self;
    SetBounds(50, 46, 41, 13);
    Caption := ''; //SDrivesCap;
    Font.Color := clRed;
    Font.Size := 16;
  end;

  OKButton := TButton.Create(Self);
  with OKButton do
  begin
    Parent := Self;
    SetBounds(172, 204, 77, 27);
    TabOrder := 4;
    OnClick := OKClick;
    Caption := ''; //SOKButton;
    ModalResult := 1;
    Default := True;
    Enabled := False;
  end;

  CountDownTmr := TTimer.Create(Self);
  with CountDownTmr do
  begin
    OnTimer := OnCountDownTmr;
    Interval := 1000;
  end;

  FormCreate(Self);
end;

procedure TCountDownDlg.FormCreate(Sender: TObject);
begin
end;

procedure TCountDownDlg.OKClick(Sender: TObject);
begin
   if Prompt then
     ModalResult := 0;
     {
  if Prompt and
    (MessageDlg(SConfirmCreateDir, mtConfirmation, [mbYes, mbNo],
    0) <> mrYes) then

    ModalResult := 0;
    }
end;

procedure TCountDownDlg.OnCountDownTmr(Sender: TObject);
begin
  if Count < 1 then
  begin
    CountDownTmr.Enabled := False;
    OKButton.Enabled := True;
    OKButton.Caption := 'OK';
  end
  else
  begin
    OKButton.Caption := Format('剩余 %d 秒', [Count]);
    Count := Count - 1;
  end;
end;

function MyMessageDlg(var MsgStr: string; Interval: Longint): Boolean;
var
  D: TCountDownDlg;
begin
  D := TCountDownDlg.Create(Application);
  try
    D.Count := Interval div 1000;
    D.CountDownTmr.Enabled := True;
    D.MessageLbl.Caption := MsgStr;

    if D.MessageLbl.Width>D.Width then
    D.Width:= D.MessageLbl.Width+50;
    { scale to screen res }
    if Screen.PixelsPerInch <> 96 then
    begin
      D.ScaleBy(Screen.PixelsPerInch, 96);
      D.Left := (Screen.Width div 2) - (D.Width div 2);
      D.Top := (Screen.Height div 2) - (D.Height div 2);

    end;
    Result := D.ShowModal = mrOK;

  finally
    D.Free;
  end;
end;

end.                                                 
使用在单元中 uses CountDownCtrl

直接调用MyMessageDlg('my test message',10000); //第一个是消息,第二个是倒计时秒数.这个只是简单的例子,可以根据自己的要求完善功能.

相关阅读 >>

Delphi datasnap使用ipv6

Delphi ansi与unicode编码互转

Delphi 判断端口(port)是否被占用

Delphi 如何判断当前网卡是物理网卡

Delphi更改android亮度

Delphi android调用授权管理打开通知监听服务

Delphi twebbrowser打开paypal出错

Delphi 62进制转10进制

Delphi xe firemonkey的几个特色属性

Delphi ios 保持设备开机状态

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



打赏

取消

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

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

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

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

评论

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