Further to this post whose accepted answer remains very cryptic:
@Button1.OnClick := pPointer(Cardinal(pPointer( procedure (sender: tObject) begin ((sender as TButton).Owner as TForm).Caption := 'Freedom to anonymous methods!' end )^ ) + $0C)^;
I wonder wether it is possible to devise a simplest and elegant way akin to:
Button.OnClick :=
AnonProc2NotifyEvent (
procedure (Sender: TObject)
begin
((Sender as TButton).Owner as TForm).Caption := 'Freedom to anonymous methods!'
end
);
so as to attain the same purpose and where AnonProc2NotifyEvent is a method of the owner of Button with the following signature:
TOwnerOfButton = class(TForm)
Button: TButton;
...
private
...
protected
function AnonProc2NotifyEvent(aProc: TProc<TObject>): TNotifyEvent;
public
...
end;
Is that feasible and if so how to implement it ?
This will do the job readily enough:
The
Ownerparameter inAnonProc2NotifyEventis so that the lifetime of the wrapper object can be managed. Without something like that you would leak instances ofTNotifyEventWrapper.Pass as
Owner, the component to which you are connecting the event. For example:So, when the button is destroyed, the
TNotifyEventWrapperwill also be destroyed. The wrapper object must live at least as long as the object to whose events it is associated. And so the choice ofButton1as the owner is the natural and obvious one.