Following David's recommendations in the comments, I created a small registration form that will be created at startup, containing a memo, timer, and the following OnTimer event:
procedure TForm1.Timer1Timer(Sender: TObject); function logtomemo(aHandle: HWND): TWinControl; var form: TWinControl; begin form := findControl(ahandle); if form <> nil then memo1.Lines.Add(format('handle %d - form %s', [ahandle, form.Name])); result := form; end; var handle: HWND; form: TWinControl; begin memo1.Clear; handle := application.ActiveFormHandle; repeat form := logtomemo(handle); handle := GetWindow(handle, GW_OWNER); until (handle = application.MainFormHandle) or (form = nil); logtomemo(handle); end;
By clicking on the button, I noticed that as soon as I clicked outside of my application, our splash shape appeared as the only form on the list. (Historically, our splash screen was used only to release after Application.Run, because they used some other links on it for some reason - they are no longer needed until my time).
Changing the lifetime of the pop-up screen, which should be destroyed before Application.Run, seems to have sorted out the problem - something that I would never have guessed would have been the cause in a million years.
I need final confirmation that it will not appear again when I get rid of this small form of debugging, but I hope the problem that upset me for several days is now sorted - thanks!
Edit
As I already noted in my editing and comments on this question, the above debug file did not work, as the presence of a new form “fixed” the problem. Changing the code so that the output was sent to the event log or text file, rather than requiring a form, also did not find anything - all forms in order Z remained in place.
In the end, I was able to fix the symptom, not the cause, by adding the following code to Application.OnModalEnd
if Application.ModalLevel = 0 then Windows.SetActiveWindow(Application.MainFormHandle);
This successfully returns the active window to the main form after closing the last modal dialog.
This can have some side effects if the user expects a modeless form that is not the main form for focus recovery, but our application architecture really does not match this structure and Application.MainFormOnTaskbar , the main form will not hide other forms (if they are not uneven)