I'm looking for easy way to create TNotifyEvent hook/wrapper So I got an idea to create it as object to make stuff easier
But I have no idea how to attach / swap method pointers correctly... :/
Maybe anyone of You has done similar things before?
Here is the skeleton of my class
TNotifyEventHook = class
private
NotifyEvent: ???????;
OldProc, NewProc: ???????;
FContinueEventChain: Boolean;
procedure Wrapper(Sender: TObject);
public
constructor Create(OriginalNotifyEvent: ???????; ChainNotifyEvent???????);
destructor Destroy; override;
property ContinueEventChain: Boolean read FContinueEventChain write FContinueEventChain default True;
end;
constructor TNotifyEventHook.Create(OriginalNotifyEvent: ???????; ChainNotifyEvent: ???????);
begin
NotifyEvent := ??????? // save
OldProc := ???????
NewProc := ???????
NotifyEvent := ??????? // redirect NotifyEvent to Wrapper
end;
destructor TNotifyEventHook.Destroy;
begin
??????? // detach chain
end;
procedure TNotifyEventHook.Wrapper(Sender: TObject);
begin
if Assigned(NewProc) then
NewProc(Sender);
if FContinueEvenChain and Assigned(OldProc) then
OldProc(Sender);
end;
I'd be really grateful for help... Or maybe just better ideas?
This is not gonna work because you cannot store the reference to an event property itself in a variable or parameter. The NotifyEvent ís the OldProc/NewProc. In other words: the thing that is stored in an event property is the event handler which get called when the property is fired from within the objects internals. So a valid
TNotifyEventinstance only tells which routine gets called, not to which object nor property the routine is linked to.If your question would be limited to one specific event, e.g.
TControl.OnClick, then the following works. Otherwise, I suggest you use a multi-cast event model, for example likeTApplicationEventsdoes.Copy this for each specific event you would like to intercept. Beware that this code example does not safeguard the possibility that
FControldoes not exist anymore at this object's destruction. Therefore it would be best to descend fromTComponentand useFreeNotification.