如何控制child个窗体在MDI窗体中的显示顺序

How can I control the display order of child Forms in a MDI Form

当我使用 MDI 表单时遇到问题。我的源代码是这样的:

private void menuItem1_Click(object sender, EventArgs e)
    {
        Form[] charr = this.MdiChildren;
        int i = 0;            
        foreach (Form chform in charr)
        {
            chform.Dock = DockStyle.Top;                
        }
        this.LayoutMdi(System.Windows.Forms.MdiLayout.TileHorizontal);
    }

child个表单的个数多于3个,为了调用LayoutMdi()方法后能正确显示,我不得不设置Dock属性 的所有 child 表格到 DockStyle.Top

调用LayoutMdi(MdiLayout.TileHorizontal)后,点击第一个child窗体的标题栏,这个child窗体显示在MDI[=34=的底部] 自动.

我希望单击的 child 表单保持其原始位置。
这个问题有什么想法吗?

查看链接的问题 - 建议设置 Dock 属性 以调整 MDIChild 表单位置 - 以及当前报告的行为,可能更可取在不借助自动功能的情况下定义 MDIChild 表单的布局。

这允许执行任何看起来合适的布局逻辑。

例子中,MDIChildren.Height是相对于MDIParent.ClientSize.Height和打开的数量MDIChildren计算的,然后乘以一个值:在示例代码中乘以2,基本措施的两倍。

此乘数允许非常精确地定义 MDICHildrenHorizontal Tile Height。当然,您可以实现一些其他逻辑,仅在至少打开 3 个时才应用乘数 MDIChildren.

所有 MDIChildren 都重新调整大小以匹配 MDIParent.Width 和计算的 Height,然后按名称排序并从上到下定位。

设置不同的 HorizontalTileHeightMultiplier 值以查看 MDIChildrenMDIParent.ClientArea 中的位置(MdiClient)。
此乘数也可以用作应用程序中的自定义 属性,可供其用户使用,允许自定义表格平铺。

布局代码作为私有方法提供,因此可以很容易地在不同的事件处理程序中使用它来 perform/maintain 选定的布局(例如 MDIParent.Resize)。
如果需要,这种方法也可以很容易地用于替换 MdiLayout.TileVertical

private float horizontalTileHeightMultiplier = 2;

private void menuItem1_Click(object sender, EventArgs e)
{
    TileHorizontal()
}

private void TileHorizontal()
{
    int openedForms = Application.OpenForms.Count - 1;
    if (openedForms < 2) return;

    int startLocation = 0;
    int childrenHeight = 
        (int)((ClientSize.Height / openedForms) * horizontalTileHeightMultiplier);

    List<Form> children = MdiChildren.OrderBy(f => f.Name).ToList();
    foreach (Form child in children)
    {
        child.Size = new Size(ClientSize.Width - SystemInformation.VerticalScrollBarWidth - 4, childrenHeight);
        child.Location = new Point(0, startLocation);
        startLocation += childrenHeight;
    }
}