Ian*_*oyd 6 delphi destructor delphi-5 tframe
我怎样才能在Delphi中模拟一个OnDestroy事件TFrame?
我简单地添加了一个constructor和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;
Run Code Online (Sandbox Code Playgroud)
这个问题是,当我的析构函数运行时,框架上的控件已经被破坏并且不再有效.
原因在于包含表单的析构函数,它用于触发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;
Run Code Online (Sandbox Code Playgroud)
当表单的析构函数运行时,我的框架对象的析构函数被调用.问题是,为时已晚.表单调用DestroyWindowHandle,要求Windows销毁表单的窗口句柄.这会递归地破坏所有子窗口 - 包括我框架上的窗口.
因此,当我的帧destructor运行时,我尝试访问不再处于有效状态的控件.
我怎样才能在Delphi中模拟一个OnDestroy事件TFrame?
您需要添加WM_DESTROY处理程序并检查ComponentState中的csDestroying,以便它仅在实际销毁时捕获,而不是在重新创建句柄时捕获.
type
TCpFrame = class(TFrame)
private
FOnDestroy: TNotifyEvent;
procedure WMDestroy(var Msg: TWMDestroy); message WM_DESTROY;
published
property OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
end;
procedure TCpFrame.WMDestroy(var Msg: TWMDestroy);
begin
if (csDestroying in ComponentState) and Assigned(FOnDestroy) then
FOnDestroy(Self);
inherited;
end;
Run Code Online (Sandbox Code Playgroud)
这只有在实际创建了框架的窗口句柄时才有效.没有其他好的挂钩点,所以如果你想确保它总是被调用,你需要在WMDestroy中设置一个标志,如果没有被击中则回退到在析构函数中调用它.
窗口句柄本身都在WM_NCDESTROY中被清除,WM_NCDESTROY在所有后代WM_DESTROY消息返回后被调用,因此表单及其所有childens的句柄在此时仍然有效(忽略在表单的OnDestroy中释放的任何内容) .