Form.Parent和StartPosition.CenterParentParent、Form、CenterParent、StartPosition

2023-09-08 08:47:22 作者:不带听你道谢

我要完全以另一种形式面前表现的一种形式,这使我对以下问题。

I need to show a form exactly in front of another form, this lead me to the following question.

为什么一个表单可以有一个起始位置为 CenterParent 虽然具有字段 this.Parent 等于空?

How come a form can have a start position as CenterParent while having the field this.Parent equals to null?

它必须知道父母为了给自己正确定位,这确实如此,但字段设置。这很奇怪。我失去了一些东西?

It must know the parent in order to position itself correctly, which it does, but the Parent field is not set. This is odd. Am I missing something?

        Form2 f = new Form2();
        f.ShowDialog();

这就是所有我做的子窗体上。父被设置为默认的Windows的位置。无论身在何处我谨父窗体,孩子会显示在父母的心。

Thats all I do on the child form. The parent is set to default windows position. No matter where I move the parent form, the child is shown in the center of the parent.

推荐答案

有关传递,通过API调用创建对话框的所有者信息(你可以看到,在ShowDialog的(在反射IWin32Window所有者)方法):

The information about the owner is passed to the created dialog via the API call (you can see that in Reflector within the ShowDialog(IWin32Window owner) method):

UnsafeNativeMethods.SetWindowLong(new HandleRef(this, base.Handle), -8, new HandleRef(owner, handle));

当没有在指定的ShowDialog老板叫所有者变量是通过GetActiveWindow API调用calcualated:

When there is no owner specified in ShowDialog call the owner variable is calcualated via the GetActiveWindow API call:

IntPtr activeWindow = UnsafeNativeMethods.GetActiveWindow();
IntPtr handle = (owner == null) ? activeWindow : Control.GetSafeHandle(owner);

要访问该业主˚F对话框的形式,你可以使用GetWindowLong API调用:

To get access to the Owner f dialog form you can use the GetWindowLong API call:

IntPtr ownerHandle = NativeMethods.GetWindowLong(nonModalForm.Handle, -8);