如何在 Delphi 中模拟OnDestroy
事件?TFrame
我在我的框架中添加了一个constructor
and destructor
,认为这就是TForm
:
TframeEditCustomer = class(TFrame)
...
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
...
end;
constructor TframeEditCustomer.Create(AOwner: TComponent)
begin
inherited Create(AOwner);
//allocate stuff
end;
destructor TframeEditCustomer.Destroy;
begin
//cleanup stuff
inherited Destroy;
end;
这样做的问题是,当我的析构函数运行时,框架上的控件已被破坏并且不再有效。
原因在于包含表单的析构函数,它用于触发OnDestroy
事件:
destructor TCustomForm.Destroy;
begin
...
if OldCreateOrder then DoDestroy; //-->fires Form's OnDestroy event; while controls are still valid
...
if HandleAllocated then DestroyWindowHandle; //-->destroys all controls on the form, and child frames
...
inherited Destroy; //--> calls destructor of my frame
...
end;
当窗体的析构函数运行时,我的框架对象的析构函数被调用。问题在于为时已晚。窗体调用DestroyWindowHandle
,它要求 Windows 销毁窗体的窗口句柄。这会递归地破坏所有子窗口 - 包括我框架上的那些。
因此,当我的框架destructor
运行时,我尝试访问不再处于有效状态的控件。
如何在 Delphi 中模拟OnDestroy
事件?TFrame