我需要在另一个表单的正前方显示一个表单,这让我想到了以下问题。
为什么表单的起始位置为 CenterParent
而字段 this.Parent
等于 null?
它必须知道父级才能正确定位自己,它确实这样做了,但未设置 Parent
字段。这很奇怪。我错过了什么吗?
Form2 f = new Form2();
f.ShowDialog();
这就是我在子表单上所做的一切。父级设置为默认窗口位置。无论我将父窗体移到哪里,子窗体都显示在父窗体的中心。
最佳答案
有关所有者的信息通过 API 调用传递给创建的对话框(您可以在 Reflector 的 ShowDialog(IWin32Window owner) 方法中看到):
UnsafeNativeMethods.SetWindowLong(new HandleRef(this, base.Handle), -8, new HandleRef(owner, handle));
当 ShowDialog 调用中没有指定所有者时,owner
变量通过 GetActiveWindow API 调用计算:
IntPtr activeWindow = UnsafeNativeMethods.GetActiveWindow();
IntPtr handle = (owner == null) ? activeWindow : Control.GetSafeHandle(owner);
要访问 Owner f 对话框表单,您可以使用 GetWindowLong API call :
IntPtr ownerHandle = NativeMethods.GetWindowLong(nonModalForm.Handle, -8);
关于c# - Form.Parent 和 StartPosition.CenterParent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10945107/