我认为你应该使用以下方法来完成:
OnDblClick
事件。FOnInternalDblClick
(名称不重要)的方法,与TNotifyEvent
兼容。FOnInternalDblClick
内部,执行复合组件的OnDblClick
。FOnInternalDblClick
分配给你希望管理事件的每个组件的OnDblClick
事件。示例代码:
TMyCompoundComponent = class(TCustomPanel)
protected
FOnDblClick : TNotifyEvent;
procedure FOnInternalDblClick(ASender : TObject);
public
constructor Create(AOwner : TComponent); override;
published
property OnDblClick : TNotifyEvent read FOnDblClick write FOnDblClick;
end;
constructor TMyCompoundComponent.Create(AOwner : TComponent);
begin
inherited;
//Lab1.OnDblClick := FOnInternalDblClick;
//Lab2.OnDblClick := FOnInternalDblClick;
//...
end;
procedure TMyCompoundComponent.FOnInternalDblClick(ASender : TObject);
begin
if(Assigned(FOnDblClick))
then FOnDblClick(ASender);
end;
注意:
在复合组件的OnDblClick
事件处理程序中,ASender
参数将是内部组件(Lab1
,Lab2
,Lab3
...)。如果您希望将复合组件本身作为ASender
参数接收,可以通过传递Self
而不是ASender
来更改FOnInternalDblClick
方法:
procedure TMyCompoundComponent.FOnInternalDblClick(ASender : TObject);
begin
if(Assigned(FOnDblClick))
then FOnDblClick(Self);
end;
您的两个请求都是可行的。这取决于您想要做什么。如果您想要为每个子项单独编写程序代码,则需要创建三个额外的公开属性,并将它们映射到相应的子组件属性。像这样(仅展示一个子组件 - 重复另外2个):
type
TMyPanelForm1 = class( TPanel )
private
fLabel1, fLabel2 : TLabel;
fImage : TImage;
procedure SetLabel1DblClick(const Value: TNotifyEvent);
function GetLabel1DblClick: TNotifyEvent;
public
constructor Create(AOwner: TComponent); override;
published
property OnLabel1DblClick : TNotifyEvent
read GetLabel1DblClick write SetLabel1DblClick;
end;
...
function TMyPanelForm1.GetLabel1DblClick: TNotifyEvent;
begin
Result := fLabel1.OnDblClick;
end;
procedure TMyPanelForm1.SetLabel1DblClick(const Value: TNotifyEvent);
begin
fLabel1.OnDblClick := Value;
end;
另一方面,如果你希望控件表现为一个统一的控件,其中所有三个子控件“继承”主组件的双击事件,你只需这样进行级联分配:
TMyPanelForm2 = class( TPanel )
private
fLabel1, fLabel2 : TLabel;
fImage : TImage;
function GetOnDblClick: TNotifyEvent;
procedure SetOnDblClick(const Value: TNotifyEvent);
public
constructor Create(AOwner: TComponent); override;
published
property OnDblClick : TNotifyEvent
read GetOnDblClick write SetOnDblClick;
end;
...
function TMyPanelForm2.GetOnDblClick: TNotifyEvent;
begin
Result := inherited OnDblClick;
end;
procedure TMyPanelForm2.SetOnDblClick(const Value: TNotifyEvent);
begin
inherited OnDblClick := Value;
fLabel1.OnDblClick := Value;
fLabel2.OnDblClick := Value;
fImage.OnDblClick := Value;
end;