倒计时对话框,自己写得比较乱
来源:互联网 发布:ubuntu开机启动服务 编辑:程序博客网 时间:2024/05/16 15:42
unit CountDownCtrl platform;{$R-,T-,H+,X+}interfaceuses Windows, Messages, SysUtils, Classes, Controls, Graphics, Forms, Menus, StdCtrls, Buttons, ExtCtrls;function MyMessageDlg(var MsgStr: string; Interval: Longint): Boolean;implementationuses 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);beginend;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); //第一个是消息,第二个是倒计时秒数.这个只是简单的例子,可以根据自己的要求完善功能.