c# - Form.Parent 和 StartPosition.CenterParent

标签 c# winforms .net-3.5

我需要在另一个表单的正前方显示一个表单,这让我想到了以下问题。

为什么表单的起始位置为 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/

相关文章:

c# - 优化 Entity Framework

c# - 对查询字符串参数使用 html.encode

c# - 如何模拟 IP 地址 - 需要设备模拟器

c# - ListView 文本被截断——如何显示项目的完整文本?

c# - 有没有更有效的方法在 C# 中生成 4000 万个字母数字唯一随机字符串

c# - Excel VSTO + C# .NET + 创建数据透视表

c# - 在文本框 C# 中键入时搜索

c# - 将 System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials 与 SAM 一起使用时出现奇怪错误

.net - Windows 10 上的 SQL Server 2014

c# - ASP.NET 产品网站的路由规则