PRocedure TComponent.InsertComponent(AComponent: TComponent);
begin
AComponent.ValidateContainer(Self);
ValidateRename(AComponent, '', AComponent.FName);//看是否有重名的對象如果有則發異常
Insert(AComponent);//在自己的子組件列表里加入AComponent的引用
AComponent.SetReference(True);
if csDesigning in ComponentState then
AComponent.SetDesigning(True);
Notification(AComponent, opInsert);//給自己發一個通知消息
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);//遞歸調用查是否有重名的組件
end;
procedure TComponent.ValidateContainer(AComponent: TComponent);
begin
AComponent.ValidateInsert(Self);
end;
procedure TComponent.ValidateInsert(AComponent: TComponent);
begin
end;
procedure TComponent.Insert(AComponent: TComponent);//我們可以看到他的子組件的引用是用TLIST來保存的
begin
if FComponents = nil then FComponents := TList.Create;
FComponents.Add(AComponent);
AComponent.FOwner := Self;//給剛剛加上的主件賦上擁有者屬性
end;
destructor TComponent.Destroy; file://可以看到每一個組件都維護一個FFREENOTIFILES的列表
begin
Destroying;
if FFreeNotifies <> nil then
begin
while Assigned(FFreeNotifies) and (FFreeNotifies.Count > 0) do
TComponent(FFreeNotifies[FFreeNotifies.Count - 1]).Notification(Self, opRemove);
FreeAndNil(FFreeNotifies);//如果自己析構的時候,對自己FFREENOTIFILES里面引用的對象調用Notification,把自己析構造的事件告訴他們
end;
DestroyComponents;
if FOwner <> nil then FOwner.RemoveComponent(Self);
inherited Destroy;
end;
procedure TComponent.FreeNotification(AComponent: TComponent);//通過這個過程可以證實FFREENOTIFILES是一個列表這個列表維護自己析構的時候給那些對象通知
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 then
begin
FFreeNotifies.Add(AComponent);
AComponent.FreeNotification(Self);
end;
end;
Include(FComponentState, csFreeNotification);
end;
新聞熱點
疑難解答