如何发现我的delphi应用程序当前是否有模态窗口?

时间:2021-01-11 07:24:14

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.

我在我的Delphi MDI应用程序中运行了一个计时器,如果后台发生变化,我想用它来弹出一条消息。但是,当应用程序在前台有一个模式对话框时,我不希望弹出该消息,因为用户无法对其进行任何操作。

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

所以我想知道的是如何检查应用程序中是否存在模态对话框?

5 个解决方案

#1


9  

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.

我用Delphi 4测试过,对我有用。

[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.

[编辑]:但你应该考虑是否弹出一个表格并窃取焦点是一个好主意。这取决于您的应用程序,但如果用户当前正在编辑字段中输入内容或使用鼠标执行某些操作,则可能会破坏其工作流程。

#2


6  

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

从Delphi 2005开始,您在TApplication上有一个ModalLevel属性。它计算应用程序中打开的模态表单的数量。

#3


2  

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.

也许解决方案是实际弹出一个不会窃取焦点的提示。可点击的提示在某处可见,但不是太具侵略性。因此,如果用户想要采取行动,或者他们可以完成他们正在做的事情,那么就采取行动。或者完全忽略它。

#4


2  

use AnyPopup() function

使用AnyPopup()函数

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

关于GetLastActivePopup()。它可能返回值与hWnd参数时相同

  • The window identified by hWnd was most recently active.
  • 由hWnd标识的窗口最近是活动的。

  • The window identified by hWnd does not own any pop-up windows.
  • 由hWnd标识的窗口不拥有任何弹出窗口。

  • The window identifies by hWnd is not a top-level window, or it is owned by another window.
  • hWnd标识的窗口不是*窗口,或者由另一个窗口拥有。

#5


1  

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

今天用户histrio在另一个线程中正确回答,仅监控模态Delphi表单是不够的; Windows也可以有模态对话框。

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

他在另一个主题中的回答告诉你如何检查。

--jeroen

#1


9  

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.

我用Delphi 4测试过,对我有用。

[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.

[编辑]:但你应该考虑是否弹出一个表格并窃取焦点是一个好主意。这取决于您的应用程序,但如果用户当前正在编辑字段中输入内容或使用鼠标执行某些操作,则可能会破坏其工作流程。

#2


6  

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

从Delphi 2005开始,您在TApplication上有一个ModalLevel属性。它计算应用程序中打开的模态表单的数量。

#3


2  

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.

也许解决方案是实际弹出一个不会窃取焦点的提示。可点击的提示在某处可见,但不是太具侵略性。因此,如果用户想要采取行动,或者他们可以完成他们正在做的事情,那么就采取行动。或者完全忽略它。

#4


2  

use AnyPopup() function

使用AnyPopup()函数

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

关于GetLastActivePopup()。它可能返回值与hWnd参数时相同

  • The window identified by hWnd was most recently active.
  • 由hWnd标识的窗口最近是活动的。

  • The window identified by hWnd does not own any pop-up windows.
  • 由hWnd标识的窗口不拥有任何弹出窗口。

  • The window identifies by hWnd is not a top-level window, or it is owned by another window.
  • hWnd标识的窗口不是*窗口,或者由另一个窗口拥有。

#5


1  

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

今天用户histrio在另一个线程中正确回答,仅监控模态Delphi表单是不够的; Windows也可以有模态对话框。

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

他在另一个主题中的回答告诉你如何检查。

--jeroen