问题描述
我的应用程序是基于模态窗体。主窗体使用ShowModal打开一个窗体,此窗体使用ShowModal打开另一个窗体,因此我们已经堆叠了模态窗体。有时候会出现一个问题,当我们以新的形式调用ShowModal时,它隐藏在以前的表单之上,而不是在顶部显示。按Alt + Tab后,表单返回顶部,但这不是很好的解决方案。您是否遇到这个问题,您是如何处理的?编辑:
我使用Delphi 7。
你没有提到哪个版本的Delphi ...
较新的Delphi版本向TCustomForm添加了两个新属性:PopupMode和PopupParent。将您的模态对话框的PopupParent设置为创建该对话框的表单,确保子表单保持在其父对象的顶部。它通常会解决您正在描述的问题。
我认为这对属性是在Delphi 2006中添加的,但它可能是2005年。他们绝对在Delphi 2007和更高版本。
编辑:看到你使用的是Delphi 7之后,我唯一的建议是,在显示你的模态窗体的代码中,你禁用创建它的形式,并在返回时重新启用。这应该阻止创建窗口接收输入,这可能有助于保持Z顺序正确。
像这样的东西可能会工作(未经测试,因为我不再使用D7):
procedure TForm1.ShowForm2;
begin
Self.Enabled:= False;
尝试
与TForm2.Create(nil)做
开始
尝试
如果ShowModal = mrOk然后
//返回确定。做一点事;
终于
免费;
结束
结束
finally
Self.Enabled:= True;
结束
结束
如果Form2创建了一个模态窗口(如前所述),只需重复该过程即可禁用Form2 ,创建Form3并以模态显示,并在返回时重新启用Form2。确保使用try..finally,如我所示,所以如果在模态窗体出现问题,创建表单总是被重新启用。
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?
EDIT:
I use Delphi 7.
You didn't mention which version of Delphi...
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.
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.
EDIT: 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.
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;
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时,窗体隐藏在其他窗体之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!