FMX Message window appears in background

Message dialogs are occasionally appearing in the background behind the application window, instead of in a modal top most window

Alt tabbing a few times sometimes (not sure if always) bring the message window to the front

I suspect this issue is related to a long running routine calling Application.ProcessMessages to refresh the screen and to allow the user to cancel the operation. I expect that converting the code to a thread might resolve the issue, but I want to avoid that if possible as it will require a lot of re-testing

Is there any workaround for this to force the message window to always appear at the top ?

I am displaying the message using vcl.dialogs.MessageDlg

Environment - Delphi 10.1, Windows 10

The only two causes I know of are

  1. Keydown event is used to action things, allowing the previous window to receive keyup, thus changing focus.
  2. The user doubleclicks, again, causing focus to change.

My solution has been to move the dialog window to the front in the idle loop. It still isnt foolproof.

Thanks Rohit

I forgot to mention that this was a FireMonkey application, not VCL.

I havent found a workaround so far, so Ive decided to implement message dialogs using my own custom form. It is fairly easy to do and gives me control over this issue. I already use a messaging unit for all messages, so I just have to redirect that to use the new form instead of the standard FMX dialogs.

So, my issue is now resolved by using my custom form, however I am still interested in other workarounds so please post if you know of anything

You are most likely going to find the answer here, even though it applies to VCL the same problem would be in FMX. MessageBox is hidden by a modal form - VCL - Delphi-PRAXiS [en]

Hi Scott,

We have had this problem a lot over the years. The problem with this call is that you don’t get to pass the parent window through – which I would suggest you should almost always do.

The equivalent windows API has the option of passing a window handle for the parent window, or passing NULL to make the message dialog application modal. From our experience application modal dialogs can have the problem (I have seen the behaviour you describe in versions of outlook for example). The application modal behaviour seems to be a bit flaky in widows, particularly if you have a modeless windows along side your main window, but for some reason Delphi seemed to really exacerbate the problem.

We ended up writing our own wrapper to a call to CreateMessageDialog, setting the dialog popup parent and calling ShowModal. And we always pass the parent window through. This has made a massive improvement to the number of occasions we get the ‘window behind’ issue you seem to be describing.









Thanks @Duncan . Ill try that out

In the meantime I am using a clunky workaround where I created my own custom form to display messages. That is good enough for the application in questions, but I want to look into this further for other applications so Ill look into your suggestions