The best way to learn Delphi is to see Delphi's VCL original code love to see his mechanism. This article will look at his mechanism. {Tcomponent} constructor tComponent.create (Aowner: tComponent); begin fcomponentStyle: = [csinheritable ]; If aowner <> nil damponent (self);
Procedure Tcomponent.insertComponent (Acomponent: Tcomponent); Begin Acomponent.validateContainer (self); ValidateRename (Acomponent, '', Acomponent.fname); // See if there is a renameless object if there is an exception INSERT (Acomponent); / / Join Acomponent's reference to Acomponent (TRUE); if Csdesigning In ComponentState Then ACOMPONENT.SETDESIGNING (TRUE); NOTINTDESIGNING (TRUE); // Send yourself a notification message END;
procedure TComponent.ValidateRename (AComponent: TComponent; const CurName, NewName: string); begin if (AComponent <> nil) and not SameText (CurName, NewName) and (AComponent.Owner = Self) and (FindComponent (NewName) <> nil ) then raise EComponentError.CreateResFmt (@SDuplicateName, [NewName]); if (csDesigning in ComponentState) and (Owner <> nil) then Owner.ValidateRename (aComponent, CurName, NewName); // recursive call to check if there are duplicate of Component end;
Procedure Tcomponent.validateContainer (ACOMPONENT: TComponent); Begin Acomponent.ValidateInsert (Self); END;
Procedure Tcomponent.validateInsert (ACOMPONENT: TComponent); BeGund;
Procedure Tcomponent.insert (ACOMPONENT: TComponent); // We can see that the reference to his subcomponents is a Begin if fcomponents = nil damponents: = nil damponents: = tList.create; fcomponents.add (attomponent); Acomponent. FOWNER: = Self; // 加 主 主 主 主;;
destructor TComponent.Destroy; file: // can be seen that each component maintains a list of begin Destroying a FFREENOTIFILES; if FFreeNotifies <> nil then begin while Assigned (FFreeNotifies) and (FFreeNotifies.Count> 0) do TComponent (FFreeNotifies [FFreeNotifies .Count - 1]). Notification (Self, OpRemove); FreeAndnil (FFreenotifies); // When you destructure, call NOTIFICATION to the object referenced by you ffreenotifiles, tell them your own restructive event; destroyComponents; If FOWNER <> nil damponent (self); inherited destroy; end; procedure tcomponent.freenotification (ACOMPONENT: TCOMPONENT); // can confirm that ffreenotifiles is a list of this list to protect yourself to those objects notification begin if (Owner = nil) or (AComponent.Owner <> Owner) then begin // Never acquire a reference to a component that is being deleted assert (not (csDestroying in (ComponentState AComponent.ComponentState)));. if NOT ASSIGNED (FFreenotifies) THEN FFREENOTIFIES: = TList.create; if ffreenotifies.indexof (ACOMPONENT) <0 The begin ffreenotifies.add (Acomponent); Acomponent.Freenotification (S END; End; End; Include (fcomponentState, csfreenotification);