procedure Register;
begin
RegisterComponents('Welkin', [TAutoCloseButton]);
end;
{ TAutoCloseButton }
constructor TAutoCloseButton.Create(AOwer: TComponent);
begin
inherited Create(AOwer);
iTickTime:=10;
bAutoClose:=false;
end;
destructor TAutoCloseButton.Destroy;
begin
if FHandle<>0 then
KillTimer (FHandle,M_TIMER) ; //销毁定时器
inherited;
end;
procedure TAutoCloseButton.SetAutoClose(value:boolean);
begin
bAutoClose:=value;
if bAutoClose then
begin
//创建一个定时器
FHandle:=Self.Handle;
SetTimer(FHandle,M_TIMER,1000, nil);
strCaption:=Self.Caption;
end
else
begin
KillTimer(FHandle, M_TIMER);
FHandle:=0;
end;
end;
procedure TAutoCloseButton.SetTickTime(value: integer);
begin
if value>=0 then
iTickTime:=value
else
iTickTime:=1;
end;
procedure TAutoCloseButton.Timer(var Msg: TWMTIMER);
begin
//捕获定时器消息
if bAutoClose then
begin
Self.Caption:=strCaption+'('+IntToStr(iTickTime)+')';
if iTickTime<=0 then
begin
KillTimer (FHandle,M_TIMER) ; //销毁定时器
SendMessage(Self.Parent.Handle,WM_CLOSE,0,0);
end;
Dec(iTickTime);
end;
end;
// 这个还有进度条的。不想要可以去掉
procedure TForm1.Button1Click(Sender: TObject);
var
AMsgDialog: TForm;
AProgressBar: TProgressBar;
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 10 seconds';
Height := 150;
with AProgressBar do begin
Name := 'Progress';
Parent := AMsgDialog;
Max := AMsgDialog.Tag; //seconds
Step := 1;
Top := 100;
Left := 8;
Width := AMsgDialog.ClientWidth - 16;
end;
with ATimer do begin
Interval := 1000;
OnTimer := DialogTimer;
end;