Accessing protected event of TWinControl
Asked Answered
A

1

3

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;

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

Panel1.OnMouseDown:=SuperObject1.DoSomething;

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;

simply does not work.

Can it be done by RTTI? How?

Thanx

Actinolite answered 5/8, 2013 at 20:47 Comment(4)
You can create a class (inherited from TControls) publishing the protected Events and assign the events by casting to this class. e.g. THack(MyClass).OnMouseDown := ... to do so your class will have to be a descendant of TControl.Brahmani
@bummi: the same for you... :-)Actinolite
I guess this is a dupe many times over: google.com/m?q=delphi+protected+hack+site%3Astackoverflow.comQuadrivium
Be aware there already is a Delphi SuperObject that parses and writes JSONCyrilla
E
8

You don't need RTTI.

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);

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;

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

Estrous answered 5/8, 2013 at 21:5 Comment(1)
I think you might be a genius... ;-)Actinolite

© 2022 - 2024 — McMap. All rights reserved.