views:

823

answers:

5

I've got a timer running in my Delphi MDI application and I'd like to use it to pop up a message if something changes in the background. But I don't want that message to pop up when the the application has a modal dialog in the foreground because the user couldn't do anything about it.

So what I'd like to know is how can I check for the existence of a modal dialog in my application?

+4  A: 

You could try with this code:

var
  ActForm: TCustomForm;
begin
  ActForm := Screen.ActiveForm;
  if (ActForm = nil) or not (fsModal in ActForm.FormState) then begin

  end;
end;

I tested with Delphi 4, works for me.

[EDIT]: But you should really think about whether popping up a form and stealing focus is a good idea. It depends on your application, but if a user is currently entering something into an edit field, or doing something with the mouse, then this might break their workflow.

mghie
Right, in this case the user has the option to not receive pop-ups and the default is set to not.
Peter Turner
+2  A: 

Perhaps the solution is to actually pop up a hint which doesn't steal focus. A clickable hint somewhere visible, but not too invasive. Thus, if the user wants to take action they can, or they can finish off what they were doing, then take action. Or perhaps ignore it altogether.

Steve
Thanks for the comment and I agree.
Peter Turner
+3  A: 

Since Delphi 2005 you have a ModalLevel property on TApplication. It counts the number of Modal forms opened in the application.

Lars Truijens
That's cool, come on over and tell my boss to upgrade!
Peter Turner
There are also a few events: TApplication.OnModalBegin and TApplication.OnModalEnd (See this question http://stackoverflow.com/questions/3306260/delphi-detecting-if-my-app-has-a-modal-dialog-open)
Name
A: 

Today user histrio correctly answered in another thread that just monitoring modal Delphi forms is not enough; Windows can also have modal dialogs.

His answer in another thread shows you how to check for that.

--jeroen

Jeroen Pluimers
+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 answering this here as well.
Jeroen Pluimers