views:

418

answers:

1

Please explain the difference between:

ChildForm := TForm.CreateParented(AOwner)

ChildForm := TForm.CreateParentedControl(AOwner)

ChildForm := TForm.Create(AOwner); 
ChildForm.ParentWindow := AOwner.Handle

This example may be complicated and convoluted, I'd really just like an overview of when people use the different kinds of Create methods for forms.

Delphi 7 help tells me that I should use CreateParented(AOwner.Handle) and ParentWindow := AOwner.handle with non-VCL controls or across DLL's. Until yesterday I just set Parent := AOwner, and I have absolutely no idea why this stopped working.

(Maybe I just need to reboot my computer)

+3  A: 

We have Components. They are visible or invisible items on a form or a datamodule. Each component can have an owner that is responsible for the eventual destruction. If there is no owner, you must take care of the destruction yourself.

We have Controls, which are components that are visible. They also have a Parent which contains the control. For example a Panel is the parent of a button on that panel.

We also have WinControls which are controls that link to windows objects. They also have a handle of the parent window.

So:

  1. TMyControl.CreateParented constructor CreateParented(ParentWindow: HWnd);

    This is used to create a control from which the parent window is provided by an handle. It creates the control without owner and sets the parentwindow to ParentWindow.

  2. TMyControl.CreateParentedControl class function CreateParentedControl(ParentWindow: HWND): TWinControl;

    Creates the control, without owner, sets the parentwindow to ParentWindow and returns it.

  3. TMyControl.Create(AOwner: TComponent)

    Creates a control with owner set to AOwner.

  4. TMyControl.ParentWindow := AOwner.Handle;

    Sets the parentwindow (handle) to the handle of AOwner.

Gamecat