本文整理自网络,侵删。
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》频道 >>