找不到MainMenu的OnClick事件,写了一个Button的,你看情况改改吧!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls;
type
TMyButton=Class(TButton)
private
FOnClick: TNotifyEvent;
Procedure SClick(Sender:TObject);
Procedure MyClickEvent;
Public
constructor Create(AOwner: TComponent); override;
published
property OnClick: TNotifyEvent read FOnClick write FOnClick;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
MyButton:TMyButton;
Procedure MyButtonClick(Sender:TObject);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyButton }
constructor TMyButton.Create(AOwner: TComponent);
begin
inherited;
inherited OnClick:=SClick;
//请注意因为父类TButton也有一个OnClick事件,自己也有一个OnClick事件,
//所以这里一定要inherited 父类的OnClick事件.
end;
procedure TMyButton.MyClickEvent;
begin
ShowMessage('MyClickEvent');
end;
procedure TMyButton.SClick(Sender: TObject);
begin
if Assigned(FOnClick) then//如果OnClick赋了值则执行赋值的事件否则执行MyClickEvent
FOnClick(Self)
else
MyClickEvent;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyButton:=TMyButton.Create(Self);
MyButton.Parent:=Self;
// MyButton.OnClick:=MyButtonClick;
//给不给MyButton的OnClick事件赋值将有两种不同的提示.
end;
procedure TForm1.MyButtonClick(Sender: TObject);
begin
ShowMessage('');
end;
end.