Delphi

本类阅读TOP10

·游戏外挂设计技术探讨①
·如何使用Delphi设计强大的服务器程序
·分布式网络考试系统原型分析及实现
·用DLL方式封装MDI子窗体。
·使用HOOK随心监视Windows
·Delphi 水晶报表打包解决
·url编码与解码工具附代码
·工人线程中关闭窗体的实现
·hdsi2.0 sql注入部分抓包分析语句
·Borland Delphi 2005 下载

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
通过原代码学习TComponent类的机制(1)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

   学习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;




相关文章

相关软件




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有