TForm.Create(Self)的self是做什么用的?

ShapeRock 2004-12-28 07:01:18
1.TForm.Create(Self)的self是做什么用的?
2.form1 :=TForm.Create(Self);
form1 :=TForm.Create(nil);
上两创建时有什么区别,运行在表现在哪里不同?
...全文
162 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
g961681 2004-12-28
  • 打赏
  • 举报
回复
procedure TCustomForm.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
case Operation of
opInsert:
begin
if AComponent is TCustomActionList then
begin
if FActionLists = nil then FActionLists := TList.Create;
FActionLists.Add(AComponent);
end
else if not (csLoading in ComponentState) and (Menu = nil) and
(AComponent.Owner = Self) and (AComponent is TMainMenu) then
Menu := TMainMenu(AComponent);
end;
opRemove:
begin
if (FActionLists <> nil) and (AComponent is TCustomActionList) then
FActionLists.Remove(AComponent)
else
begin
if Menu = AComponent then Menu := nil;
if WindowMenu = AComponent then WindowMenu := nil;
if ObjectMenuItem = AComponent then ObjectMenuItem := nil;
end;
end;
end;
if FDesigner <> nil then
FDesigner.Notification(AComponent, Operation);
end;
这是来自vcl的代码。
g961681 2004-12-28
  • 打赏
  • 举报
回复
跟Notification函数有关。
当为self时,需要通知当前form的所有组件,刚才的form已经创建了,并且可以不用手工释放,在self释放时,同时会释放其下属的刚创建的form的。如果你把刚创建的form手工释放的话,同样还会再通知self里的所有组件,它已经释放了。
在这一方面的的一般用法:如果生命周期很短的form,我会
with Tform.create(nil) do
try
ShowModal;
finally
Free;
end;
如果生命周期比较长的,比如在create时创建,destory时释放的窗体,我会create(application);
syl79 2004-12-28
  • 打赏
  • 举报
回复
form1 :=TForm.Create(Self);
指定Owner为self指向的窗口,则self指向的窗口Free时由该窗口自动Free生成的form1。self也可以为当前的application。
form1 :=TForm.Create(nil);
未指定owner,得程序自己调用Free过程。
yangfan369 2004-12-28
  • 打赏
  • 举报
回复
就是当前form
boatzm 2004-12-28
  • 打赏
  • 举报
回复
AOwner is the owner of the TCustomForm object.

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧