You need to override the protected CloseQuery virtual method in your main form. When this fires, you know that the application is going down. But the legacy implementation calls CloseQuery for MDI children before the OnCloseQuery event OnCloseQuery in the main form.
Here is the implementation of TCustomForm CloseQuery :
function TCustomForm.CloseQuery: Boolean; var I: Integer; begin if FormStyle = fsMDIForm then begin Result := False; for I := 0 to MDIChildCount - 1 do if not MDIChildren[I].CloseQuery then Exit; end; Result := True; if Assigned(FOnCloseQuery) then FOnCloseQuery(Self, Result); end;
Please note that MDI children receive their CloseQuery notifications before this for Self , i.e. basic form.
So, in your main form, you need:
type TMainForm = class(TForm); private FCloseQueryExecuting: Boolean; protected function CloseQuery: Boolean; override; public property CloseQueryExecuting: Boolean read FCloseQueryExecuting; end;
and then an implementation that looks like this:
function TMainForm.CloseQuery: Boolean; begin FCloseQueryExecuting := True; try Result := inherited CloseQuery; finally FCloseQueryExecuting := False; end; end;
MDI children can then check the status of the main FCloseQueryExecuting in their OnCloseQuery events.
source share