欢迎光临
我们一直在努力

通过原代码学习tcomponent类的机制(1)_delphi教程

建站超值云服务器,限时71元/月

 学习DELPHI最好的方法是看DELPHI的VCL原代码爱看他的机制本文就类TComponent的原代码来看他的机制.
{ TComponent }
constructor TComponent.Create(AOwner: TComponent);
begin
  FComponentStyle := [csInheritable];
  if AOwner <> nil then AOwner.InsertComponent(Self);
end;

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;

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 通过原代码学习tcomponent类的机制(1)_delphi教程
分享到: 更多 (0)