Form.Parent和StartPosition.CenterParent

Ody*_*dys 13 c# .net-3.5 winforms

我需要在另一个表单前面显示一个表单,这引出了以下问题.

为什么一个表单可以有一个起始位置,因为CenterParent该字段this.Parent等于null?

它必须知道父级才能正确定位,它确实如此,但是Parent没有设置字段.这很奇怪.我错过了什么吗?

        Form2 f = new Form2();
        f.ShowDialog();
Run Code Online (Sandbox Code Playgroud)

这就是我在孩子表上所做的一切.父级设置为默认窗口位置.无论我在何处移动父表单,子表示都显示在父表单的中心.

Dmi*_*ryG 8

有关所有者的信息将通过API调用传递给创建的对话框(您可以在ShowDialog(IWin32Window所有者)方法中的Reflector中看到):

UnsafeNativeMethods.SetWindowLong(new HandleRef(this, base.Handle), -8, new HandleRef(owner, handle));
Run Code Online (Sandbox Code Playgroud)

如果在ShowDialog调用中没有指定所有者,owner则通过GetActiveWindow API调用计算变量:

IntPtr activeWindow = UnsafeNativeMethods.GetActiveWindow();
IntPtr handle = (owner == null) ? activeWindow : Control.GetSafeHandle(owner);
Run Code Online (Sandbox Code Playgroud)

要访问Owner f对话框表单,您可以使用GetWindowLong API调用:

IntPtr ownerHandle = NativeMethods.GetWindowLong(nonModalForm.Handle, -8);
Run Code Online (Sandbox Code Playgroud)