I have a Delphi 2006 application that can display a modal dialog box in response to an error condition. It seems that he is in a state where one of these modal dialogs is open, positioned in front of the main form, but not one of the forms responds to messages. Pressing the button gives "bonk". The application works fine, the user interface updates the main form, but you can not do anything. I think that there is the most likely other modal dialogue in the main form. Whether this is one of mine or one of Windows, I have no idea.
Other items:
- The application responds to OK shortcuts. One of these short circuits gracefully closes the application and it worked. Since then, I have not been able to reproduce the situation.
- The application has a tray icon. This responds to right clicks. If I hide the application here, the main form minimizes and leaves the displayed modal dialog still without focus. If I restore the main form, everything happens as it was, and not a single window has focus. Alt-Tab have similar results. Platform
- is Windows 7
- I call DisableProcessWindowsGhosting before creating any forms.
I open modal dialogs with
ModalDialog.PopupParent := MainForm ; ModalDialog.ShowModal ;
I put off these error dialogs if other modal dialogs open:
if (Application.ModalLevel = 0) then {open modal dialog}
My question has two parts:
Is there a way to programmatically find out which window has focus? Then I could take some action for this scenario, or as a last resort, I could provide them with a shortcut to the foreground or take some evasive actions (depending on the dialog), for example, set ModalResult to mrCancel.
How could such a situation arise? Usually, when I get a modal dialog behind the main form (I can do this by opening the modal dialog, minimizing the application from the tray icon, and then restoring the application again - the main form of the application is restored before the dialog, the dialog still retains focus), I can bring it back to the forefront by clicking on the tray icon or closing it with the Esc , but in this case it does not work.
** UPDATE **
Misha fix worked separately from non-delphi dialogs like TSaveDialog. I was able to get them to work by adding Application.ModalPopupMode := pmAuto ; immediately before calling Execute .
By βgetting it to work,β I mean that the save dialog was ahead after the following sequence:
- open save dialog
- Minimize application from tray icon
- restore application from tray icon
whereas it was behind the main form without ModalPopupMode := pmAuto .
Therefore, I hope that these changes will help the (not yet recreated) problem.
rossmcm
source share