delphi 应用程序工厂_了解Delphi应用程序中的所有者与父代

delphi 应用程序工厂

Every time you place a panel on a form and a button on that panel you make an "invisible" connection. The Form becomes the owner of the Button, and the Panel is set to be its parent.

每次将面板放置在窗体上并在该面板上放置按钮时,都会建立“不可见”连接。 表单成为Button的所有者 ,并且Panel被设置为其父对象

Every Delphi component has an Owner property. The Owner takes care of freeing the owned components when it is being freed.

每个Delphi组件都有一个Owner属性。 所有者负责在释放组件时将其释放。

Similar, but different, the Parent property indicates the component that contains the "child" component.

相似但不同的Parent属性指示包含“子”组件的组件。

父母 ( Parent )

Parent refers to the component that another component is contained in, such as TForm, TGroupBox or a TPanel. If one control (parent) contains others, the contained controls are child controls of the parent.

父级是指另一个组件所包含的组件,例如TForm,TGroupBox或TPanel。 如果一个控件(父控件)包含其他控件 ,则包含的控件是父控件的子控件。

Parent determines how the component is displayed. For example, the Left and Top properties are all relative to the Parent.

父级确定组件的显示方式。 例如,Left和Top属性都相对于Parent。

The Parent property can be assigned and changed during run-time.

可以在运行时分配和更改Parent属性。

Not all components have the Parent. Many forms do not have a Parent. For example, forms that appear directly on the Windows desktop have Parent set to nil. A component's HasParent method returns a boolean value indicating whether or not the component has been assigned a parent.

并非所有组件都具有父级。 许多表格没有父母。 例如,直接在Windows桌面上显示的表单的“父级”设置为nil。 组件的HasParent方法返回一个布尔值,该布尔值指示是否已将组件分配给父对象。

We use the Parent property to get or set the parent of a control. For example, place two panels (Panel1, Panel2) on a form and place one button (Button1) on the first panel (Panel1). This sets Button's Parent property to Panel1.

我们使用Parent属性获取或设置控件的父级。 例如,在窗体上放置两个面板(Panel1,Panel2),然后在第一个面板(Panel1)上放置一个按钮(Button1)。 这会将Button的Parent属性设置为Panel1。

 Button1.Parent := Panel2; 

If you place the above code in the OnClick event for the second Panel, when you click Panel2 the button "jumps" from Panel1 to Panel2: Panel1 is no longer the Parent for the Button.

如果将上面的代码放在第二个Panel的OnClick事件中,则当您单击Panel2时,该按钮将从Panel1跳转到Panel2:Panel1不再是Button的父级。

When you want to create a TButton at run-time, it is important that we remember to assign a parent - the control that contains the button. For a component to be visible, it must have a parent to display itself within.

当您想在运行时创建一个TButton时,记住记住要分配一个父级(包含该按钮的控件)非常重要。 为了使组件可见,它必须具有父项才能在中显示自身

ParentThis和ParentThat ( ParentThis and ParentThat )

If you select a button at design time and look at the Object Inspector you'll notice several "Parent-aware" properties. The ParentFont, for example, indicates whether the Font used for the Button's caption is the same as the one used for the Button's parent (in the previous example: Panel1). If ParentFont is True for all Buttons on a Panel, changing the panel’s Font property to Bold causes all Button's caption on the Panel to use that (bold) font.

如果在设计时选择一个按钮,然后查看“对象检查器”,您将注意到几个“父母感知”属性。 例如, ParentFont指示用于Button的标题的Font是否与用于Button的父字体的字体相同(在前面的示例中为Panel1)。 如果对于面板上的所有按钮的ParentFont为True,则将面板的Font属性更改为粗体会导致面板上所有按钮的标题都使用该(粗体)字体。

控制属性 ( Controls Property )

All components that share the same Parent are available as part of the Controls property of that Parent. For example, Controls may be used to iterate over all the children of the windowed control.

共享同一父级的所有组件都可以作为该父级的Controls属性的一部分使用。 例如,控件可用于迭代窗口控件的所有子级

The next piece of code can be used to hide all the contained components on Panel1:

下一段代码可用于隐藏Panel1上所有包含的组件:

 for ii := 0 to Panel1.ControlCount - 1 do
   Panel1.Controls[ii].Visible := false;

rick俩 ( Tricking Tricks )

Windowed controls have three basic characteristics: they can receive the input focus, they use system resources, and they can be parents to other controls.

窗口控件具有三个基本特征:它们可以接收输入焦点,使用系统资源以及可以成为其他控件的父级。

For example, the Button component is a windowed control and cannot be the parent to some other component - you can't place another component on it. The thing is that Delphi hides this feature from us. An example is the hidden possibility for a TStatusBar to have some components like TProgressBar on it.

例如,Button组件是一个窗口控件,不能成为其他某个组件的父级-您不能在其上放置另一个组件。 事实是,Delphi向我们隐藏了此功能。 一个示例是TStatusBar隐含一些组件(例如TProgressBar)的可能性。

所有权 ( Ownership )

First, note that a Form is the overall Owner of any components that reside on it (positioned on the form at design-time). This means that when a form is destroyed, all the components on the form are also destroyed. For example, if we have an application with more that one form when we call the Free or Release method for a form object, we do not have to worry about explicitly freeing all of the objects on that form—because the form is the owner of all its components.

首先,请注意,表单是驻留在其上的任何组件的整体所有者(在设计时位于表单上)。 这意味着在销毁表单时,表单上的所有组件也会销毁。 例如,如果我们为一个表单对象调用Free或Release方法时拥有一个具有多个表单的应用程序,则不必担心显式释放该表单上的所有对象,因为该表单是该表单的所有者所有组件。

Every component we create, at design or run time, must be owned by another component. The owner of a component—the value of its Owner property—is determined by a parameter passed to the Create constructor when the component is created. The only other way to re-assign the Owner is using the InsertComponent/RemoveComponent methods during run-time. By default, a form owns all components on it and is in turn owned by the Application.

我们在设计或运行时创建的每个组件都必须由另一个组件拥有。 组件的所有者(其Owner属性的值)由创建组件时传递给Create构造函数的参数确定。 重新分配所有者的唯一其他方法是在运行时使用InsertComponent / RemoveComponent方法。 默认情况下,表单拥有其上的所有组件,然后又由应用程序拥有。

When we use the keyword Self as the parameter for the Create method—the object we are creating is owned by the class that the method is contained in—which is usually a Delphi form.

当我们使用关键字Self作为Create方法的参数时(我们创建的对象由该方法所在的类拥有)通常是Delphi形式。

If on the other hand, we make another component (not the form) the owner of the component, then we are making that component responsible for disposing of the object when it is destroyed.

另一方面,如果我们使另一个组件(而不是表单)成为该组件的所有者,则我们使该组件负责在对象被销毁时处置该对象。

As like any other Delphi component, custom made TFindFile component can be created, used and destroyed at run time. To create, use and free a TFindFile component at run, you can use the next code snippet:

与其他任何Delphi组件一样,可以在运行时创建,使用和销毁定制的TFindFile组件。 要在运行时创建,使用和释放TFindFile组件 ,可以使用下一个代码片段:

 uses FindFile;

...
...
var FFile : TFindFile;
procedure TForm1.InitializeData;
begin //form ("Self") is the Owner of the component  //there is no Parent since this  //is an unvisible component.
  FFile := TFindFile.Create(Self) ;
  ...
 end;

Note: Since the FFile is created with an owner (Form1), we don't need to do anything to free the component—it will be freed when the owner is destroyed.

注意:由于FFile是使用所有者(Form1)创建的,因此我们无需执行任何操作即可释放组件-在销毁所有者时将其释放。

组件属性 ( Components Property )

All components that share the same Owner are available as part of the Components property of that Owner. The following procedure is used to clear all the Edit components that are on the form:

共享同一所有者的所有组件都可以作为该所有者的Components属性的一部分使用。 以下过程用于清除表单上的所有“编辑”组件:

 procedure ClearEdits(AForm: TForm) ;
var
   ii : Integer;
 begin
   for ii := 0 to AForm.ComponentCount-1 do
   if (AForm.Components[ii] is TEdit) then TEdit(AForm.Components[ii]).Text := '';
end; 

“孤儿” ( "Orphans" )

Some controls (such as ActiveX controls) are contained in non-VCL windows rather than in a parent control. For these controls, the value of Parent is nil and the ParentWindow property specifies the non-VCL parent window. Setting ParentWindow moves the control so that it is contained in the specified window. ParentWindow is set automatically when a control is created using the CreateParented method.

某些控件(例如ActiveX控件)包含在非VCL窗口中,而不包含在父控件中。 对于这些控件,Parent的值为nil ,并且ParentWindow属性指定非VCL父窗口。 设置ParentWindow会移动控件,使其包含在指定的窗口中。 使用CreateParented方法创建控件时,将自动设置ParentWindow。

The truth is that in most cases you do not need to care about Parents and Owners, but when it comes to OOP and component development or when you want to take Delphi one step forward the statements in this article will help you to take that step faster.

事实是,在大多数情况下,您不需要关心父级和所有者,但是当涉及到OOP和组件开发时,或者当您希望向前推进Delphi时,本文中的语句将帮助您更快地执行此步骤。 。

翻译自: https://www.thoughtco.com/owner-vs-parent-in-delphi-applications-1058218

delphi 应用程序工厂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值