我是这样做的:
--------------------------------
TOnClickEvent=procedure(Sender: TObject) of object; TPic=class(TComponent)
private
fActive:boolean;
fOnClick:TOnClickEvent;
picPanel:TPanel;
pImage:TImage;
fLabel:TLabel;
procedure SetCaption(s:string);
function ReadCaption:string;
procedure SetActive(const value:boolean);
procedure SetTop(newtop:integer);
function ReadTop:integer;
procedure SetLeft(newleft:integer);
function ReadLeft:integer;
public
procedure SetPicture(pic:TGraphic);
procedure Show;
constructor Create(AOwner:TComponent);override;
published
property Caption:string read ReadCaption write SetCaption;
property Top:integer read ReadTop write SetTop;
property Left:integer read ReadLeft write SetLeft;
property Active:boolean read fActive write SetActive;
property OnClick:TOnClickEvent read fOnClick write fOnClick;
end;然后在 Create中将 pImage.OnClick 和 OnClick 联系起来但是我在引用该类的时候,没反应的
--------------------------------
TOnClickEvent=procedure(Sender: TObject) of object; TPic=class(TComponent)
private
fActive:boolean;
fOnClick:TOnClickEvent;
picPanel:TPanel;
pImage:TImage;
fLabel:TLabel;
procedure SetCaption(s:string);
function ReadCaption:string;
procedure SetActive(const value:boolean);
procedure SetTop(newtop:integer);
function ReadTop:integer;
procedure SetLeft(newleft:integer);
function ReadLeft:integer;
public
procedure SetPicture(pic:TGraphic);
procedure Show;
constructor Create(AOwner:TComponent);override;
published
property Caption:string read ReadCaption write SetCaption;
property Top:integer read ReadTop write SetTop;
property Left:integer read ReadLeft write SetLeft;
property Active:boolean read fActive write SetActive;
property OnClick:TOnClickEvent read fOnClick write fOnClick;
end;然后在 Create中将 pImage.OnClick 和 OnClick 联系起来但是我在引用该类的时候,没反应的
当构件用户为事件指定处理子程序后,事件便成为指向该子程序的指针。事件的定义方式如下:
type
private
FOnClick: TNotifyEvent;{声明事件变量以保存过程指针}
published
property OnClick:TNotifyEvent read FOnClick write FOnClick;
end;
此例正是 Delphi标准控件中 Click事件的定义方式。可以看出,除了 OnClick被定义为过程类型外,其定义格式与一般属性的
直接访问格式几乎完全相同。 Delphi预定义了所有标准事件的过程类型及标准事件所引发的虚方法。其中, Click事件将引发如下
虚方法:
procedure TControl.Click;
begin
if Assigned( OnClick) then OnClick( Self);
{以下是默认处理部分}
end;
其中, Assigned函数检验 OnClick是否已分配了事件处理过程。如果返回值为 True,则调用用户指定的事件处理过程。通过
重载此虚方法,可以修改 Click事件的处理方式。在重载的方法中,一般应先调用用户处理程序,然后再安排后续处理。在本例中,首行代码应当是 inherited Click。
需要注意的是,构件用户不一定会给事件指定处理程序,因此事件不能定义为函数类型,否则可能会指向返回值类型不定的空函
数。如果需要事件处理过程返回某个值,可以借助 var参数。调用用户程序之前应确保此参数包含有效返回值,以免用户未指定事件处理过程时出错。
事件就是属性!建议你看看美国人写的delphi6从入门到精通82元
那个书专门为控件开发人写的
(也就是说点击鼠标是怎么会触发OnClick属性呢?)我继承的是TComponent,在该类中又找不到Click过程