哈..这个我已经完成了,自己简单的写了下..功能也还实现了..现在贴出来!
注: 你可对这个组件进行任意修改,但很诚恳的希望能把你修改后的东西,送我一份,相互学习,才会有进步! 谢谢!(liuweiming504@163.com)
unit AutoCloseButton;
interface
uses
SysUtils, Classes, StdCtrls,Messages,Windows;
const
M_TIMER=1; //定时器的名字
type
TAutoCloseButton = class(TButton)
private
{ Private declarations }
strCaption:string;
iTickTime:integer; //间隔时间(秒)
bAutoClose:boolean;
FHandle:HWND;
procedure SetAutoClose(value:boolean);
procedure SetTickTime(value: integer);
procedure Timer(var Msg:TWMTIMER);message WM_TIMER;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwer:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
Property AutoClose: boolean read bAutoClose write SetAutoClose default false;
Property TickTime: integer read iTickTime write SetTickTime default 10;
end;
procedure Register;
implementation
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;
end.