这是一个普通窗体的代码,我想谁都看得懂的:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}end.我想问的是:在这个例子中,既然TForm1类里定义了一个TButton类型的对象Button1,为什么在TForm1类的定义中没有看到创建和释放Button1的代码呢?
TForm1从TForm继承而来,继承了TForm创建和释放代码(实际上TForm的创建和释放代码也可能是继承
TCustomForm或其它先辈创建和释放代码),无须再额外写一个.
Application.CreateForm(TForm1, Form1);
.dfm文件里有流代码
object Form1: TForm1
Left = 192
Top = 114
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 96
Top = 56
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
end
end
你看一下,对于此窗体的所有控件定义都在这里。
var
I: Integer;
Instance: TControl;
begin
Destroying;
if FDockSite then
begin
FDockSite := False;
RegisterDockSite(Self, False);
end;
FDockManager := nil;
FDockClients.Free;
if Parent <> nil then RemoveFocus(True);
if FHandle <> 0 then DestroyWindowHandle;
I := ControlCount;
while I <> 0 do
begin
Instance := Controls[I - 1];
Remove(Instance);
Instance.Destroy;
I := ControlCount;
end; FBrush.Free;
{$IFDEF LINUX}
if FObjectInstance <> nil then WinUtils.FreeObjectInstance(FObjectInstance);
{$ENDIF}
{$IFDEF MSWINDOWS}
if FObjectInstance <> nil then Classes.FreeObjectInstance(FObjectInstance);
{$ENDIF}
inherited Destroy;
end;tform是从TWinControl继承来的,上面为TWinControl.Destroy的部分代码,红色为TWinControl对所属组件的释放.
(回复内容太短了!)
这些控件在TForm1释放时,由Owner去释放