Delphi模态窗体位置

发布于 2024-11-06 17:03:49 字数 170 浏览 0 评论 0原文

我有一个表单正在被另一个表单打开。

我将位置设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳公公是暖光 2024-11-13 17:03:49

我对你的问题有点困惑,所以我会澄清我要在这里解决的问题!...

我认为你想要做的是

当 Form2 打开时,它位于 Form1 的中央,并且 Form1 被隐藏。

当 Form2 关闭时,会显示 Form1(正是它隐藏的位置)。

我认为你想要做的是让 Form1 显示 Form2 关闭的位置。

所以我猜你有一些像...这样的代码

procedure TForm1.ButtonClick(Sender: TObject);
begin
  Form2.ShowModal;
end;

,并且你期望 Form2 更新 form1 的位置,因为你将 Form2 的位置设置为 poOwnerFormCenter

好吧,如果我猜对了所有这些,那么你需要做的就是当 Form2 关闭时更新 Form1 的头寸是

procedure TForm1.ButtonClick(Sender: TObject);
begin
  Form2.ShowModal;
  Left := Form2.Left;
  Top := Form2.Top;
end;

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...

procedure TForm1.ButtonClick(Sender: TObject);
begin
  Form2.ShowModal;
end;

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

procedure TForm1.ButtonClick(Sender: TObject);
begin
  Form2.ShowModal;
  Left := Form2.Left;
  Top := Form2.Top;
end;
柠檬心 2024-11-13 17:03:49

问题是您正在重复使用模态形式的相同实例。设置位置仅在第一次显示表单时有效。您必须在这里选择:

选项 1

您可以在每次关闭时销毁模态表单。实现此目的的方法之一是在表单的 OnClose 事件上添加这一行:

Action = caFree;

当然,这意味着您每次都必须从调用方重新创建模式表单。

选项 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:

Action = caFree;

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.

怪我入戏太深 2024-11-13 17:03:49

(我猜)这是因为您每次显示表单时都会重新创建表单。也就是说,

with TForm2.Create(nil) do
  try
    ShowModal;
  finally
    Free;
  end;

因为每次显示 TForm2 类时都会创建一个新实例,并在表单关闭时销毁它,所以位置会发生变化;事实上,新的 TForm2 对象不可能记住任何先前的 TForm2 对象的位置。它们是两个不同的对象(是的,同一类,但这并不重要)!

最简单的解决方案是将 TForm2 添加到项目选项中的“自动创建表单”列表中。默认情况下它就在那里,但如果您手动创建它(正如我认为您所做的那样,并且如上面的代码片段所示),您应该将其从自动创建的表单列表中删除......

IDE Screenshot

然后确保 Unit1 使用 Unit2,以便您可以访问来自驻留在 Unit1 中的 Form1Unit2 中的全局 Form2 变量。编辑 Unit1 时,按 Alt+F11 即可执行此操作。

然后,您可以通过执行以下操作来显示 Form2

Form2.ShowModal;

第一次显示时,它将尊重其 Position 参数,并将其自身定位在其所有者表单之上。但随后它会记住它的位置,因此第二次显示它时,它将位于第一次关闭的位置。

This is (I guess) because you recreate the form every time you display it. That is, you do

with TForm2.Create(nil) do
  try
    ShowModal;
  finally
    Free;
  end;

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 new TForm2 object cannot possibly remember the position of any previous TForm2 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...

IDE Screenshot

Then you make sure that Unit1 uses Unit2, so that you can access the global Form2 variable in Unit2 from Form1 that resides in Unit1. While editing Unit1, press Alt+F11 to do this.

Then you can just show Form2 by doing

Form2.ShowModal;

The 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文