Delphi模态窗体位置
我有一个表单正在被另一个表单打开。
我将位置设置为 poOwnerFormCenter,以便在原始表单的位置打开新表单
但是,当我移动这个新表单然后返回到原始表单时,它显示的是我第一次打开新表单时的位置,而不是显示的位置我关闭了它
我该如何解决这个问题?
谢谢!
I have a form that is being opened by another form.
I set the Position to be poOwnerFormCenter, so that the new form is opened where the original was
However, when I move this new form and then go back to the original, its shown where it was when I first opened the new form, not where I closed it
How would I fix this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对你的问题有点困惑,所以我会澄清我要在这里解决的问题!...
我认为你想要做的是
当 Form2 打开时,它位于 Form1 的中央,并且 Form1 被隐藏。
当 Form2 关闭时,会显示 Form1(正是它隐藏的位置)。
我认为你想要做的是让 Form1 显示 Form2 关闭的位置。
所以我猜你有一些像...这样的代码
,并且你期望 Form2 更新 form1 的位置,因为你将 Form2 的位置设置为
poOwnerFormCenter
好吧,如果我猜对了所有这些,那么你需要做的就是当 Form2 关闭时更新 Form1 的头寸是
I'm a bit confused by your question so I'll clarify what I'm try to solve here!...
I think what you're trying to do is
When Form2 Opens, it is positioned centrally to Form1 and Form1 is hidden.
When Form2 Closes, Form1 is shown (exactly where it was hidden).
I think you want to do is have Form1 Show where Form2 was closed.
So I'm guessing that you have some code like...
and you were expecting Form2 to update form1's position because you set Form2's position to
poOwnerFormCenter
Well if I guessed all that correctly then all you need to do to update Form1's position when Form2 closes is
问题是您正在重复使用模态形式的相同实例。设置位置仅在第一次显示表单时有效。您必须在这里选择:
选项 1
您可以在每次关闭时销毁模态表单。实现此目的的方法之一是在表单的 OnClose 事件上添加这一行:
当然,这意味着您每次都必须从调用方重新创建模式表单。
选项 2
您必须在
OnShow
事件上手动设置模态表单的位置。使用最适合您的选项。
The problem is that you are reusing the same instance of the modal form. Setting the position only works the first time you show the form. You have to options here:
Option 1
You can destroy the modal form every time it closes. One of the ways of doing that is having this line on the
OnClose
event of the form:Of course, that means you have to recreate the modal form from the caller every time as well.
Option 2
You have to manually set the modal form's position on the
OnShow
event.Use the option which best suits you.
(我猜)这是因为您每次显示表单时都会重新创建表单。也就是说,
因为每次显示 TForm2 类时都会创建一个新实例,并在表单关闭时销毁它,所以位置会发生变化;事实上,新的
TForm2
对象不可能记住任何先前的TForm2
对象的位置。它们是两个不同的对象(是的,同一类,但这并不重要)!最简单的解决方案是将
TForm2
添加到项目选项中的“自动创建表单”列表中。默认情况下它就在那里,但如果您手动创建它(正如我认为您所做的那样,并且如上面的代码片段所示),您应该将其从自动创建的表单列表中删除......然后确保
Unit1
使用Unit2
,以便您可以访问来自驻留在Unit1
中的Form1
的Unit2
中的全局Form2
变量。编辑Unit1
时,按 Alt+F11 即可执行此操作。然后,您可以通过执行以下操作来显示
Form2
:第一次显示时,它将尊重其
Position
参数,并将其自身定位在其所有者表单之上。但随后它会记住它的位置,因此第二次显示它时,它将位于第一次关闭的位置。This is (I guess) because you recreate the form every time you display it. That is, you do
Because you create a new instance of the
TForm2
class every time you show it, and destroy it when the form has closed, the position changes; indeed, the newTForm2
object cannot possibly remember the position of any previousTForm2
object. They are two different objects (yes, same class, but that doesn't matter)!The simplest solution is to add the
TForm2
to the 'auto-create forms' list in the Project Options. It is there by default, but if you create it manually (as I think you do, and as in the code snippet above), you should have removed it from the list of forms that are automatically created...Then you make sure that
Unit1
usesUnit2
, so that you can access the globalForm2
variable inUnit2
fromForm1
that resides inUnit1
. While editingUnit1
, press Alt+F11 to do this.Then you can just show
Form2
by doingThe first time it is shown, it will respect its
Position
parameter, and position itself above its owner form. But then it will remember its position, so the second time you display it, it will be right where it closed the first time.