views:

212

answers:

3

I have a Delphi 2006 app that pops up a modal alert dialog when an error condition is detected. As the check for the error condition is done in an idle handler, the dialog can pop up over the top of another modal dialog if that one happens to be displayed. This can lead to a confusing situation for the user where the application main form doesn't get focus after the alert dialog is closed because these is another obscured modal dialog.

What I would like to do is postpone the alert popups while the app has any Modal dialogs open.

I tried GetLastActivePopup (MyMainWindow.Handle) but it seems to return non-zero all the time.

+2  A: 

use AnyPopup() function

About GetLastActivePopup(). It may return value is the same as the hWnd parameter when

  • The window identified by hWnd was most recently active.
  • The window identified by hWnd does not own any pop-up windows.
  • The window identifies by hWnd is not a top-level window, or it is owned by another window.
histrio
+1 for AnyPopup; please also add that answer to http://stackoverflow.com/questions/284581/how-do-i-discover-if-my-delphi-application-currently-has-a-modal-window Note I have sligtly edited your answer to point to the on-line documentation of the functions you mention.
Jeroen Pluimers
@Jeroen: AnyPopup doc states: `This function is provided only for compatibility with 16-bit versions of Windows. It is generally not useful.` Not something to be recommended then...?
Marjan Venema
@jeroen-pluimers Thanks for edit. I will take it as rule.
histrio
@Marjan use GetLastActivePopup(hWnd) and compare its result with hWnd
histrio
@Marjan: I thought I did comment "AnyPopup/GetLastActivePopup", but I was in a hurry leaving for a client and didn't check enough. Thanks for correcting me on the AnyPopup deprecation.
Jeroen Pluimers
@Jeroen: No worries, happy to. What was that saying again "hurried speed is seldom good"? Doesn't translate very well does it? <grin>
Marjan Venema
@Marjan: "nail on the head" :->
Jeroen Pluimers
+3  A: 

See TApplication.ModalLevel or the TApplication.OnModalBegin and TApplication.OnModalEnd events. Also see this question.

Lars Truijens
Thanks Lars. Seems to work a treat. I reckon I would have a chance of finding these things out myself if the D2006 help system wasn't such a dog.
A: 

Just test

if not IsWindowEnabled(MyMainWindow.Handle) then

This would also take care of situtations when a non-Delphi dialog is acting modal, like in windows.MessageBox(Handle.... or a common dialog is showing (i.e. a TOpenDialog).

Sertac Akyuz
Unfortunately, if a non-Delphi dialog is open modally, I lose the Application OnIdle calls so the app is effectively frozen anyway.