Centralize child form does not work

时间:2016-08-19 18:38:04

标签: delphi c++builder

I have an application that works as follows: There is a main form which are open child forms for registration, search, etc. The intention is always open child forms in the center of the main form, regardless of the size of it. For this I am using the following code in the form son onCreate:

formChild->Left=(MainForm->pnlMain->Width/2);
formChild->Top=(MainForm->pnlMain->Height/2)-(formChild->Height/2);

So far everything right, the form is centered correctly, as shown in the following image:

enter image description here

当子窗体关闭时,将恢复主窗体并打开子窗体,出现以下问题: enter image description here

子表单是在第一次生成的位置生成的(使用最大化窗口)。

1 个答案:

答案 0 :(得分:5)

处理此问题的正确方法是将子表单的Position属性设置为poMainFormCenter

这将确保在第一次创建和显示子表单时,它将以MainForm为中心。

注意,如果您只是隐藏儿童表格的位置,然后稍后再次显示,则不会更新儿童表格的位置。如果您需要每次显示子窗体时始终居中,您应该在关闭时将其销毁,然后在显示之前重新创建它。