tform1.create_TForm.Create(AOwner)

tform1.create

When you create Delphi objects dynamically that inherit from TControl, such as a TForm (representing a form/window in Delphi applications), the constructor "Create" expects an "Owner" parameter:

动态创建从TControl继承的Delphi对象 (例如TForm(在Delphi应用程序中表示表单/窗口))时,构造函数“ Create”需要一个“ Owner”参数:


constructor Create(AOwn

The AOwner parameter is the owner of the TForm object. The owner of the form is responsible for freeing the form -- i.e., memory allocated by the form -- when needed. The form appears in the Components array of its owner and it is destroyed automatically when its owner is destroyed. 

AOwner参数是TForm对象的所有者。 表单的所有者负责在需要时释放表单,即由表单分配的内存。 该窗体出现在其所有者的Components数组中,并且在销毁其所有者后会自动销毁该窗体。

You have three choices for the AOwner parameter: Nil, self, and application.

AOwner参数有三个选择: Nilselfapplication

To understand the answer, you first need to know the meaning of "nil," "self" and "Application."

要理解答案,您首先需要知道“ nil”,“ self”和“ Application”的含义。

  • Nil specifies that no object owns the form and therefore the developer is responsible for freeing the created form (by calling myForm.Free when you no longer need the form)

    Nil指定没有对象拥有该表单,因此开发人员负责释放创建的表单(当您不再需要该表单时,通过调用myForm.Free)

  • Self specifies the object in which the method is called. If, for example, you are creating a new instance of a TMyForm form from inside a button's OnClick handler (where this button is placed on a MainForm), self refers to "MainForm." Thus, when the MainForm is freed, it will also free MyForm.

    Self指定在其中调用该方法的对象。 如果,例如,你是从按钮的onclick处理程序(其中该按钮被放置在MainForm中)内创建TMyForm形式的新实例, 指“MainForm的。” 因此,当MainForm被释放时,它也将释放MyForm。

  • Application specifies a global TApplication type variable created when you run your application. "Application" encapsulates your application as well as providing many functions that occur in the background of the program.

    应用程序指定在运行应用程序时创建的全局TApplication类型变量。 “应用程序”封装了您的应用程序,并提供了在程序后台出现的许多功能。

Examples:

例子:

  1. Modal forms. When you create a form to be displayed modally and freed when the user closes the form, use "nil" as the owner:

    模态形式。 当您创建要模态显示的表单并在用户关闭该表单时将其释放时,请使用“ nil”作为所有者:

    var myForm : TMyForm; begin  myForm := TMyForm.Create(nil) ;  try    myForm.ShowModal;  finally    myForm.Free; end; end;
  2. Modeless forms. Use "Application" as the owner:

    无模形式。 使用“应用程序”作为所有者:

    var

    变种

    myForm : TMyForm;

    myForm:TMyForm;

    ...

    ...

    myForm := TMyForm.Create(Application) ;

    myForm:= TMyForm.Create(Application);

Now, when you terminate (exit) the application, the "Application" object will free the "myForm" instance.

现在,当您终止(退出)应用程序时,“ Application”对象将释放“ myForm”实例。

Why and when is TMyForm.Create(Application) NOT recommended? If the form is a modal form and will be destroyed, you should pass "nil" for the owner.

为什么不建议何时以及何时不推荐TMyForm.Create(Application)? 如果该表单是模式表单并且将被销毁,则应为所有者传递“ nil”。

You could pass "application," but the time delay caused by the notification method being sent to every component and form owned or indirectly owned by the Application could prove disruptive. If your application consists of many forms with many components (in the thousands), and the form you're creating has many controls (in the hundreds), the notification delay can be significant.

您可以传递“应用程序”,但是将通知方法发送到应用程序拥有或间接拥有的每个组件和表单所导致的时间延迟可能会造成破坏。 如果您的应用程序由具有许多组件的表单(数千个)组成,并且您正在创建的表单具有许多控件(数百个),则通知延迟可能会很大。

Passing "nil" as the owner instead of "application" will cause the form to appear sooner, and will not otherwise affect the code.

将“ nil”作为所有者而不是“ application”作为所有者,将使表格尽快出现,并且不会影响代码。

However, if the form you need to create is not modal and is not created from the application's main form, then when you specify "self" as the owner, closing the owner will free the created form. Use "self" when you don't want the form to outlive its creator.

但是,如果您需要创建的表单不是模式表单,也不是从应用程序的主表单创建的,那么当您将“ self”指定为所有者时,关闭所有者将释放创建的表单。 当您不希望表单的创建者超过其创建者时,请使用“自我”。

Warning: To dynamically instantiate a Delphi component and explicitly free it sometime later, always pass "nil" as the owner. Failure to do so can introduce unnecessary risk, as well as performance and code maintenance problems.

警告 :要动态实例化Delphi组件并在以后的某个时间显式释放它,请始终以“ nil”作为所有者。 否则,可能会带来不必要的风险以及性能和代码维护问题。

In SDI applications, when a user closes the form (by clicking on the [x] button) the form still exists in the memory -- it only gets hidden. In MDI applications, closing an MDI child form only minimizes it.The OnClose event provides an Action parameter (of the TCloseAction type) you can use to specify what happens when a user attempts to close the form. Setting this parameter to "caFree" will free the form.

SDI应用程序中 ,当用户关闭表单(单击[x]按钮)时,该表单仍存在于内存中-仅被隐藏。 在MDI应用程序中,关闭MDI子窗体只会将其最小化。OnClose事件提供了( TCloseAction类型的) Action参数,您可以使用该参数指定用户尝试关闭窗体时发生的情况。 将此参数设置为“ caFree”将释放表单。

Delphi tips navigator:» Get the full HTML from the TWebBrowser component« How to Convert Pixels to Millimeters

Delphi提示导航器: »从TWebBrowser组件获取完整HTML«如何将像素转换为毫米

翻译自: https://www.thoughtco.com/tform-createaowner-aowner-1057563

tform1.create

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值