Jonas Bilinkevicius
I want my component to execute my procedure every time the main form gets
activated. I don't want the main form to execute an event. The component should do
it. It's a component I use in every application I write, and the procedure always
does the same thing. So I just want to avoid adding the same routine over and over
again in the main form. Even when it's just putting a TComponent.Execute in
Form1.FormActivate. Is it possible?
Answer:
It can be non-visual component as well in that case. You can simply drop on a form
the following component. In TActivateHook.FormActivate you put the code for all
forms, if you want any special processing for particular form, you put that code
into that form's OnActivate.
1 { ... }2 type3 TActivateHook = class(TComponent)
4 private5 OldActivate: TNotifyEvent;
6 procedure FormActivate(Sender: TObject);
7 protected8 procedure Loaded; override;
9 end;
10 11 { TActivateHook }12 13 procedure TActivateHook.FormActivate(Sender: TObject);
14 begin15 if Assigned(OldActivate) then16 OldActivate(Sender);
17 {Put your code here}18 end;
19 20 procedure TActivateHook.Loaded;
21 begin22 inherited;
23 ifnot (csDesigning in ComponentState) and (Owner <> nil) and24 Owner.InheritsFrom(TForm) then25 begin26 OldActivate := TForm(Owner).OnActivate;
27 TForm(Owner).OnActivate := FormActivate;
28 end;
29 end;