1.TForm.Create(Self)的self是做什么用的?
2.form1 :=TForm.Create(Self);
  form1 :=TForm.Create(nil);
上两创建时有什么区别,运行在表现在哪里不同?

解决方案 »

  1.   

    AOwner is the owner of the TCustomForm object.
      

  2.   

    form1 :=TForm.Create(Self);
    指定Owner为self指向的窗口,则self指向的窗口Free时由该窗口自动Free生成的form1。self也可以为当前的application。
    form1 :=TForm.Create(nil);
    未指定owner,得程序自己调用Free过程。
      

  3.   

    跟Notification函数有关。
    当为self时,需要通知当前form的所有组件,刚才的form已经创建了,并且可以不用手工释放,在self释放时,同时会释放其下属的刚创建的form的。如果你把刚创建的form手工释放的话,同样还会再通知self里的所有组件,它已经释放了。
    在这一方面的的一般用法:如果生命周期很短的form,我会
    with Tform.create(nil) do
    try
      ShowModal;
    finally
      Free;
    end;
    如果生命周期比较长的,比如在create时创建,destory时释放的窗体,我会create(application);
      

  4.   

    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的代码。