将 MDI 表单显示为模式
这听起来违背了 MDI 的本质。有时我需要将 MDI 表单 (FormStyle=fsMdiChild) 显示为模式。我还需要访问另一个 MDI 表单的 Application.CreateForm 和 OnShow 事件之间的部分,即
Application.CreateForm(Form2,TForm2); // but don't set form2's visible property true.
Form2.caption:='not working example';
Form2.SomeMagicToSetVisibleTrue;
有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于第一个问题:添加另一个构造函数,例如 CreateAsMDI,如下所示:
在普通构造函数中,只需将变量 f_blChild 设置为 false 并调用继承的 create。
您还需要两件事,而不是自我解释:
现在您可以调用表单模式(如果使用标准构造函数创建),以及作为 MDI 子项(如果使用 CreateAsMDI 创建)。
如果您将其包含在表单声明中,
您甚至可以根据表单是否为 MDI 子表单执行操作,只需询问 isChild 属性即可。
至于你的第二个问题:不要使用Application.CreateForm,而是自己创建你的表单:
这里是模态和MDI的两个创建:
我已经将这个答案翻译成网站上的一篇文章DelphiPraxis。
For your first problem: Add another constructor, for example CreateAsMDI, like this:
In the normal constructor just set the variable f_blChild to false and call the inherited create.
You need two more things, rather self explaining:
Now you can call the form modal, if created with the standard constructor, and as MDI child, if created with CreateAsMDI.
If you include this in your form's declaration
you can even do things depending on whether the form is an MDI child or not, just interrogating the isChild property.
As for your second problem: do not use Application.CreateForm, but create your form yourself:
Here the two creations for modal and MDI:
I have translated this answer form an article on the site DelphiPraxis.
最简单的方法是创建表单的一个简单子类,并
设置FormStyle = fsMDIChild
AND
Form.Visible = False
在属性检查器中 。这是经过尝试和测试的!
The simplest method is to create a trivial subclass of the form, and set
FormStyle = fsMDIChild
AND
Form.Visible = False
in the property inspector. This is tried and tested!
至少对于 Delphi 2007 和 2009,创建不可见的 MDI 子窗体很容易。对于早期的 Delphi 版本(无法在属性检查器中将
Visible
设置为False
),您只需为OnCreate
提供一个处理程序> 事件并访问类的受保护字段:这将禁用 MDI 子项的自动显示。完成其他初始化后,您只需
Show
它或将Visible
设置为True
即可。我不会尝试回答您有关模式 MDI 子表单的问题,因为这违反了 Windows 平台的约定。
At least for Delphi 2007 and 2009 creating the MDI child form invisible is easy. For the early Delphi versions (where it was impossible to set
Visible
toFalse
in the property inspector) you just have to provide a handler for theOnCreate
event and access a protected field of the class:This will disable the automatic showing of the MDI child. After you are done with your other initialisations you can simply
Show
it or setVisible
toTrue
.I will not try to answer your question about modal MDI child forms, as this violates the conventions of the Windows platform.
上面的答案实际上没有完成所需的工作。最佳答案也是错误的,原因如下:
如果您确实需要在运行时决定它是 fsMDIChild 还是 fsNormal,您需要应用以下方法。
...
No answers above actually does the job required. The best answer is wrong as well, because of following:
If you really need to decide in run-time whether it's a fsMDIChild or fsNormal you need to apply following approach.
...