我有一个 Delphi 2006 应用程序,可以在错误条件下弹出模态对话框。但它似乎陷入一种状态,其中一个这些模态对话框打开并位于主表单前面,但两个表单都不响应消息。单击任何一个会出现“咚”的声音。应用程序正常运行,UI 正在更新主表单,但您无法执行任何操作。我猜主表单下面可能还有另一个模态对话框。无论它是我的还是 Windows 的,我都不知道。
1. 是否有办法通过编程找出哪个窗口具有焦点?然后我可以针对这种情况采取一些行动,或者在必要时提供快捷键以将其置于前台或采取某些回避措施(根据对话框的情况),例如将 ModalResult 设置为 mrCancel。
2. 这种情况怎么会发生?通常当我将一个模态对话框放在主窗体后面(我可以通过让模态对话框打开,从托盘图标最小化应用程序,然后再恢复应用程序来做到这一点——应用程序主窗体恢复在对话框前面,对话框仍然保持焦点),我可以通过单击托盘图标将其再次置于前台,或者使用 Esc 键关闭它,但在这种情况下它并没有起作用。
**更新**
Misha 的修复方法有效,除了像 TSaveDialog 这样的非 Delphi 对话框。我能够通过在调用 Execute 之前添加 Application.ModalPopupMode := pmAuto;来使它们也工作。
“让它工作”的意思是,在以下序列之后保存对话框在前面:
1. 打开保存对话框。 2. 从托盘图标最小化应用程序。 3. 使用托盘图标恢复应用程序。
而没有 ModalPopupMode := pmAuto 时,它在主窗体后面。所以我希望这些更改能够解决(尚未重现的)问题。
其他要点:
- the app responds to keyboard shortcuts OK. One of these shortuts shuts down the app gracefully and this worked. I have been unable to reproduce the situation since.
- the app has a tray icon. This responds to right mouse clicks. If I minimize the app from here the main form minimizes and leaves the modal dialog displayed, still without focus. If I restore the main form, things are as they were, with neither window having focus. Alt-tab has similar results.
- platform is Windows 7
- I call DisableProcessWindowsGhosting before any forms are created
I open the modal dialogs with
ModalDialog.PopupParent := MainForm ; ModalDialog.ShowModal ;
I postpone these error dialogs if other modal dialogs are open:
if (Application.ModalLevel = 0) then {open modal dialog}
1. 是否有办法通过编程找出哪个窗口具有焦点?然后我可以针对这种情况采取一些行动,或者在必要时提供快捷键以将其置于前台或采取某些回避措施(根据对话框的情况),例如将 ModalResult 设置为 mrCancel。
2. 这种情况怎么会发生?通常当我将一个模态对话框放在主窗体后面(我可以通过让模态对话框打开,从托盘图标最小化应用程序,然后再恢复应用程序来做到这一点——应用程序主窗体恢复在对话框前面,对话框仍然保持焦点),我可以通过单击托盘图标将其再次置于前台,或者使用 Esc 键关闭它,但在这种情况下它并没有起作用。
**更新**
Misha 的修复方法有效,除了像 TSaveDialog 这样的非 Delphi 对话框。我能够通过在调用 Execute 之前添加 Application.ModalPopupMode := pmAuto;来使它们也工作。
“让它工作”的意思是,在以下序列之后保存对话框在前面:
1. 打开保存对话框。 2. 从托盘图标最小化应用程序。 3. 使用托盘图标恢复应用程序。
而没有 ModalPopupMode := pmAuto 时,它在主窗体后面。所以我希望这些更改能够解决(尚未重现的)问题。