来自:http://blog.csdn.net/lailai186/article/details/7442385

-----------------------------------------------------------------------

学习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 > ) do
TComponent(FFreeNotifies[FFreeNotifies.Count - ]).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) < then
begin
FFreeNotifies.Add(AComponent);
AComponent.FreeNotification(Self);
end;
end;
Include(FComponentState, csFreeNotification);
end;

2.7        TComponent分枝

TComponent分枝包含了所有从TComponent而不是TControl继承下来的VCL&CLX类。对于这个分枝中的对象,你在设计时刻可以直接在窗体重操作它们,但是在运行时刻它们不会为用户所见。它们是持续的对象,具有以下功能:

n        出现在组件面板中,并且在窗体重可以被改变

n        拥有和操作其它组件

n        加载和保存它们本身

TComponent引入的几个方法指明了组件在设计时刻的行为和随着组件保存哪些信息。流功能(保存窗体中对象属性的窗体文件的加载和保存)在这个分枝中出现。组件的发布属性被规定为持续的并且将自动被“流”。

TComponent分枝也引入了所有权的概念,该概念取自组件库。有两个属性支持所有权:Owner和 Components。每个组件都含有一个Owner属性指定其所有者。

一个组件可能会拥有另外的组件,在这种情况下,所有被包含组件都会出现在该组件的Components属性中。

每个组件的构造器都会包含一个参数指定新组件的所有者。

如果该引入的所有者存在,那么这个组件就会自动被添加到所有者的Components属性中。

Components属性除了用来存储所包含组件列表外,还可以提供自动释放其所有的组件。一旦一个组件有了一个所有者,那么当其所有者被释放是,它也会自动释放。

例如,因为TForm继承自TComponent,所以当一个窗体被销毁时,其所包含的所有组件都会被自动销毁并且释放其所占用的内存(当然,每个组件必须正确地设计其析构器使其能够被自动的清除)。

如果一个属性类型是Tcomponent或者其后裔,当流系统读入它时会自动创建一个其实例。如果一个属性类型是TPersistent而不是TComponent,那么流系统会通过该属性使用一个已经存在的该类型实例,并且为这个实例属性读取值。

下面是TComponent分枝的某些类:

n        TActionList,维护一个动作列表,这个动作列表提供了一系列使你的程序可以对用户输入作出反应的抽象动作

n        TMainMenu,为一个窗体提供菜单栏以及其下拉菜单

n        TOpenDialog, TSaveDialog, TFontDialog, TFindDialog, TColorDialog等等,这些类提供一些通用的对话框以显示&收集信心

n        TScreen,一个用以跟踪应用程序所创建的窗体&数据模块、活动窗体、窗体中的活动组件、屏幕的尺寸及其组成、程序可用的光标及字体等数据的类。

不需要一个可视界面的组件一般都直接源自TComponent类。如果需要创建一个诸如TTimer的组件,你可以直接继承TComponent,这些组件可以出现在组件列表中,但不是通过设计时刻的可视界面而是通过代码来访问其内在功能。尺寸的huonList,

delphi TComponent类 2的更多相关文章

  1. delphi TComponent类(1)

    来自:http://blog.csdn.net/lailai186/article/details/7442383 ------------------------------------------ ...

  2. Delphi之TComponent类

    TComponent类 TComponent类直接由TPersistent派生.TComponent的独特特征是它的属性能够在设计期间通过ObjectInspector来控制,能够拥有其他组件.非可视 ...

  3. QMetaObject感觉跟Delphi的类之类有一拼,好好学一下

    提供了一堆原来C++没有的功能,比如反射什么的...但是可能还是没有Delphi的类之类更强,因为类之类可以“创建类”.可惜我学艺不精,对“类之类”也没有完全学会.先留个爪,有空把两个东西都好好学学, ...

  4. delphi 实体类 JSON 数组

    delphi 实体类 与JSON转换,序列化 TJson REST.JSON.pas   TJson.JsonToObjectTJson.ObjectToJsonString JsonEncode O ...

  5. 比较C++、Java、Delphi声明类对象时候的相关语法

    同学们在学习的时候经常会遇到一些问题,C++.Java.Delphi他们到底有什么不一样的呢?今天我们来比较C++.Java.Delphi声明类对象时候的相关语法.希望对大家有帮助! C++中创建对象 ...

  6. Delphi 遍历类中的属性

    http://blog.csdn.net/easyboot/article/details/8004954 Delphi 遍历类中的属性 标签: delphistringbuttonclassform ...

  7. 再次深入理解delphi的类

    property WindowState: TWindowState read FWindowState write SetWindowState; {声明一个属性WindowState,它从字段FW ...

  8. DELPHI学习---类和对象(五篇)

    Classes and objects(类和对象) 类(或者类类型)定义了一个结构,它包括字段(也称为域).方法和属性:类的实例叫做对象:类的字段.方法和属性被称为它的部件(components)或成 ...

  9. 转:Delphi的类与继承(VB与delphi比较)

    既然已经做出了com程序用delphi来开发的决定,那当然就要对delphi进行一些深入的了解.有人说delphi是一个用控件堆砌起来的工具,和vb没什么两样:也有人说dephi实际上是面向过程的,他 ...

随机推荐

  1. Android 图片加载框架 Glide 的用法

    https://github.com/bumptech/glide Android图片加载框架最全解析(一),Glide的基本用法http://blog.csdn.net/guolin_blog/ar ...

  2. C++析构函数的自动调用(用于父类指针指向子类对象,内存泄漏问题)

    class A {public:A() { printf("A \n"); }~A() { printf(" ~A \n"); } // 这里不管写不写virt ...

  3. Cannot enlarge string buffer containing XX bytes by XX more bytes

    在ELK的数据库报警系统中,发现有台机器报出了下面的错误: 2018-12-04 18:55:26.842 CST,"XXX","XXX",21106,&quo ...

  4. git<撤销本地修改与回退版本>

    1. 使用 git checkout 撤销本地修改 即放弃对本地已修改但尚未提交的文件的修改,还原其到未修改前的状态. 注意: 已 add/ commit 的文件不适用个方法,应该用本文提到的第二种方 ...

  5. C陷阱与缺陷的个人知识点摘录

    编译过程的一点心得体会: .h文件其实只在预处理的过程用到,用来将类似#include <stdio.h>这样的行展开为具体内容. 那些标准库或者其他库中的函数,是在链接的过程中连接器把相 ...

  6. Excel 中 VLOOKUP() 函数小结

    应用场景: 数据仓库上游源系统的数据库表变更,现在需要拆分一部分数据出来,单独放到一张新表中.假设原表为A,新表为B,B表和A表结构大部分一样,只有字段的前缀不同,那么我们如何找出到底有哪些字段不同呢 ...

  7. openstack指南

    1.openstack官网 http://www.openstack.org/ 2.openstack源码地址 https://github.com/openstack 3.openstack的pac ...

  8. mongo ttl索引

    db.log_events.find()                                     # 查找log_events里的所有数据   db.log_events.create ...

  9. PHP基础知识之————匿名函数(Anonymous functions)

    匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应用的情况. ...

  10. android 低功耗蓝牙使用

    参考链接:http://blog.csdn.net/xubin341719/article/details/38584469 1.android 手机的低功耗蓝牙,又称BLE :BLE在andriod ...