窗体隐藏在其他窗体后面

窗体隐藏在其他窗体后面

本文介绍了调用 ShowModal 时,窗体隐藏在其他窗体后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序基于模态表单.主窗体用 ShowModal 打开一个窗体,这个窗体用 ShowModal 打开另一个窗体,所以我们有堆叠的模态窗体.有时会出现一个问题,当我们在新表单中调用 ShowModal 时,它隐藏在以前的表单后面,而不是显示在顶部.按 alt+tab 后,表单回到顶部,但这不是一个好的解决方案.您是否遇到过这个问题,您是如何处理的?

My application is based on modal forms. Main form opens one form with ShowModal, this form opens another with ShowModal, so we have stacked modal forms. There is sometimes a problem that when we call ShowModal in new form, it hides behind previous forms, instead of showing on top. After pressing alt+tab, form comes back to the top, but this is not good solution. Did You meet this problem and how did you handle it?

编辑:

我使用 Delphi 7.

I use Delphi 7.

推荐答案

你没说Delphi是哪个版本的...

You didn't mention which version of Delphi...

较新的 Delphi 版本为 TCustomForm 添加了两个新属性:PopupMode 和 PopupParent.将模态对话框的 PopupParent 设置为创建该对话框的窗体可确保子窗体位于其父窗体之上.它通常可以解决您所描述的问题.

Newer Delphi versions have added two new properties to TCustomForm: PopupMode and PopupParent. Setting PopupParent of your modal dialog to the form that's creating that dialog makes sure that the child form stays on top of it's parent. It usually fixes the problem you're describing.

我认为这对属性是在 Delphi 2006 中添加的,但可能是 2005 年.它们肯定存在于 Delphi 2007 及更高版本中.

I think this pair of properties were added in Delphi 2006, but it may have been 2005. They're definitely there in Delphi 2007 and up.

在看到您使用的是 Delphi 7 之后,我唯一的建议是,在显示您的模态表单的代码中,您禁用创建它的表单,并在返回时重新启用.这应该会阻止创建窗口接收输入,这可能有助于保持 Z 顺序正确.

After seeing you're using Delphi 7, the only suggestion I have is that, in the code that displays your modal form, you disable the form creating it, and re-enable on return. That should prevent the creating window from receiving input, which may help keep the Z-order correct.

这样的事情可能会起作用(未经测试,因为我不再使用 D7):

Something like this may work (untested, as I'm no longer using D7):

procedure TForm1.ShowForm2;
begin
  Self.Enabled := False;
  try
    with TForm2.Create(nil) do
    begin
      try
        if ShowModal = mrOk then
          // Returned OK. Do something;
      finally
        Free;
      end;
    end;
  finally
    Self.Enabled := True;
  end;
end;

如果 Form2 创建了一个模态窗口(正如您所提到的),只需重复该过程 - 禁用 Form2,创建 Form3 并以模态方式显示它,并在返回时重新启用 Form2.确保像我展示的那样使用 try..finally,这样如果模态表单出现问题,总是会重新启用创建表单.

If Form2 creates a modal window (as you've mentioned), just repeat the process - disable Form2, create Form3 and show it modally, and re-enable Form2 when it returns. Make sure to use try..finally as I've shown, so that if something goes wrong in the modal form the creating form is always re-enabled.

这篇关于调用 ShowModal 时,窗体隐藏在其他窗体后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:07