unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMybutton = class(Tbutton)
private
FOnClick:TNotifyEvent;
{ Private declarations }
protected
procedure click;override;
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
published
property OnClick : TNotifyEvent read FOnClick write FOnClick ;
{ Published declarations }
end;
TMyChildButton = class(TMyButton)
private
FOnClick:TNotifyEvent;
{ Private declarations }
protected
procedure click;override;
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
published
property OnClick : TNotifyEvent read FOnClick write FOnClick;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyZxy', [TMyButton]);
RegisterComponents('MyZxy', [TMyChildButton]);
end;
constructor TMybutton.create(AOwner:tcomponent);
begin
inherited create(AOwner);
end;
procedure TMybutton.click;
begin
showmessage('父类的CLICK');
if assigned(FOnClick) then OnClick(self);
end;
constructor TMychildbutton.create(AOwner:tcomponent);
begin
inherited create(AOwner);
end;
procedure TMychildbutton.click;
begin
inherited ;
showmessage('子类的CLICK');
//就差下面这句
if assigned(FOnClick) then OnClick(self);
end;
end.
//======================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
unit2;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
//procedure b2onclick(Sender: TObject);
procedure b2onclick(Sender: TObject);
procedure b4onclick(Sender: TObject);
procedure b3onclick(Sender: TObject);
procedure b1onclick(Sender: TObject);
private
{ Private declarations }
public
B1:TMyButton;
B2,B3,B4:TMyChildButton;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
b1:=TMyButton.Create (self);
b1.Parent :=self;//这名为什么不能放在with...do中?
//兄弟请告之
with b1 do
begin
left:= 10;
Top:=90;
Width:=100;
Height:=50;
Font.Size:=13;
Caption:='父亲&p';
OnClick := b1onclick;
end;
b2:=TMyChildButton.Create (self);
b2.Parent :=self;//这名为什么不能放在with...do中?
//兄弟请告之
with b2 do
begin
left:= 140;
Top:=90;
Width:=100;
Height:=50;
Font.Size:=13;
Caption:='子类1&c';
end;
b2.OnClick := b2onclick;
b3:=TMyChildButton.Create (self);
b3.Parent :=self;//这名为什么不能放在with...do中?
//兄弟请告之
with b3 do
begin
left:= 10;
Top:=190;
Width:=100;
Height:=50;
Font.Size:=13;
Caption:='子类2&h';
OnClick := b3onclick;
end;
b4:=TMyChildButton.Create (self);
b4.Parent :=self;//这名为什么不能放在with...do中?
//兄弟请告之
with b4 do
begin
left:= 140;
Top:=190;
Width:=100;
Height:=50;
Font.Size:=13;
Caption:='子类3&d';
OnClick := b4onclick;
end;
end;
procedure TForm1.b2onclick(Sender: TObject);
begin
showmessage('实例子类1自己的CLICK');
end;
procedure TForm1.b3onclick(Sender: TObject);
begin
showmessage('实例子类2自己的CLICK');
end;
procedure TForm1.b1onclick(Sender: TObject);
begin
showmessage('实例父亲自己的CLICK');
end;
procedure TForm1.b4onclick(Sender: TObject);
begin
showmessage('实例子类3自己的CLICK');
end;
end.
只能参考一下