Author: Lou Adler
I miss one thing in frames: The ability of performing some initialization code as I
would do in a TForm.OnCreate. So what would be the equivalent for frames?
Answer:
You can override the constructor for example. An alternative is to override the
SetParent method and do the initialization after calling the inherited method. This
way the frame will have a parent and you can then do things that require a window
handle without running into problems. I use a base frame class in my current
project that has this feature build in. The relevant parts are given below. In
descendents I just override the Initialize method.
1 { ... }2 type3 {The base class for frames}4 TPLC_BaseFrame = class(TFrame)
5 protected6 procedure SetParent(aParent: TWinControl); override;
7 public8 procedure Initialize; virtual;
9 procedure UnInitialize; virtual;
10 destructor Destroy; override;
11 end;
12 13 procedure TPLC_BaseFrame.Initialize;
14 begin15 {Override as needed in descendents}16 end;
17 18 procedure TPLC_BaseFrame.UnInitialize;
19 begin20 {Override as needed in descendents}21 end;
22 23 procedure TPLC_BaseFrame.SetParent(aParent: TWinControl);
24 var25 oldparent: TWinControl;
26 begin27 oldparent := Parent;
28 inherited;
29 if (oldparent = nil) and (aParent <> nil) then30 Initialize;
31 end;
32 33 destructor TPLC_BaseFrame.Destroy;
34 begin35 Uninitialize;
36 inherited;
37 end;