访问受保护的事件的TWinControl [英] Accessing protected event of TWinControl

查看:198
本文介绍了访问受保护的事件的TWinControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,你想分配自己的事件过程:

imagine, you want to assign your own event procedure:

procedure TSuperObject.DoSomething(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ShowMessage('Yes, I am doing');
end;

到表单上的任何TWinControl。通常如果您在表单上有Panel1(TPanel),您可以轻松实现:

to any TWinControl on the form. Normally if you have Panel1 (TPanel) on the form, you can do it easy:

Panel1.OnMouseDown:=SuperObject1.DoSomething;

但是,如果你想做到这一点,它是如何实现的?您无法访问TWincontrol的受保护成员,所以直观的答案:

But if you want to do it universally, how can it be accomplished? You can not access protected members of TWincontrol, so intuitive answer:

AnyWinControl.OnMouseDown:=SuperObject1.DoSomething;

根本不起作用。

可以通过RTTI来完成吗?如何?

Can it be done by RTTI? How?

Thanx

推荐答案

您不需要RTTI。

任何代码都隐式访问在同一单位中声明为的任何类的受保护成员。您可以通过在需要访问该类成员的单元中声明新的 TWinControl 后代来利用此功能。声明很简单:

Any code has implicit access to the protected members of any class declared in the same unit. You can take advantage of this by declaring a new TWinControl descendant in the unit that needs access to that class's members. The declaration is very simple:

type
  TProtectedWinControl = class(TWinControl);

然后键入任何其他 TWinControl descendant到这个新类型,你可以访问它的任何受保护的字段,属性和方法。 TWinControl 的受保护成员是 TProtectedWinControl (通过继承)的自动保护成员,因此当前单位可以访问它们。

Then type-cast any other TWinControl descendant to that new type, and you'll have access to any of its protected fields, properties, and methods. Protected members of TWinControl are automatically protected members of TProtectedWinControl (through inheritance), so the current unit has access to them.

TProtectedWinControl(AnyWinControl).OnMouseDown := SuperObject1.DoSomething;

请注意,这适用于受保护的成员,但不适用 em> strict 保护成员。

Note that this applies to protected members, but not strict protected members.

这篇关于访问受保护的事件的TWinControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆