如何控制MDI表单中子表单的显示顺序 [英] How can I control the display order of child Forms in a MDI Form

查看:109
本文介绍了如何控制MDI表单中子表单的显示顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 MDI 表格时,我遇到了问题.我的源代码如下:

When I use an MDI Form I have a problem. My source code just like this:

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);
    }

子窗体的数量大于3.为了在调用 LayoutMdi()方法后正确显示它们,我必须设置以下项的 Dock 属性所有子窗体都添加到 DockStyle.Top .

The numbers of child Forms is more then 3. In order to display them correctly after the LayoutMdi() method is called, I had to set the Dock property of all child Forms to DockStyle.Top.

调用 LayoutMdi(MdiLayout.TileHorizo​​ntal)后,单击第一个子窗体的标题栏,该子窗体将自动显示在 MDI 父窗体的底部.

After calling LayoutMdi(MdiLayout.TileHorizontal), clicking the Title Bar of the first child Form, this child Form is displayed at the bottom of the MDI parent automatically.

我希望所单击的子窗体保持其原始位置.
这个问题有什么主意吗?

I want that the clicked child Form maintains it's original position.
Is there any idea for this question?

推荐答案

查看链接的问题-建议在其中设置 Dock 属性以调整 MDIChild 窗体的位置-和当前报告的行为,可能更可取的是在没有自动功能的情况下定义 MDIChild 窗体的布局.

Looking at the linked question - where it was suggested to set the Dock property to adjust the MDIChild Forms position - and the currently reported behaviour, it is probably preferable to define the layout the MDIChild Forms without the help of automatic feature.

这允许执行似乎合适的任何布局逻辑.

This allows to perform any layout logic that seems appropriate.

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

In the example, the MDIChildren.Height is calculated in relation to the MDIParent.ClientSize.Height and the number of opened MDIChildren, then multiplied by a values: in the sample code by 2, twice the base measure.

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

This Multiplier allows to define the Horizontal Tile Height of the MDICHildren quite precisely. Of course, you could implement some other logic that applies the multiplier only when there are at least 3 opened MDIChildren.

调整所有 MDIChildren 的大小以匹配 MDIParent.Width 和计算出的 Height ,然后按名称排序并从顶部开始定位到底部.

All the MDIChildren are re-sized to match the MDIParent.Width and the calculated Height, then ordered by Name and positioned from top to bottom.

设置 Horizo​​ntalTileHeightMultiplier 的不同值,以查看 MDIChildren MDIParent.ClientArea ( MdiClient ).
此乘数也可以用作应用程序中的自定义属性,可供其用户使用,从而允许自定义平铺表格.

Set different values of HorizontalTileHeightMultiplier to see how the MDIChildren are positioned in the MDIParent.ClientArea (MdiClient).
This multiplier could also be used as a custom Property in the Application, available to its Users allowing a custom tiling of the Forms.

布局代码是作为私有方法提供的,因此可以轻松地在不同的事件处理程序中使用它来执行/维护选定的布局(例如, MDIParent.Resize ).
如果需要,也可以轻松地使用此方法来替换 MdiLayout.TileVertical .

The layout code is provided as a private method, so it can be easily used in different event handlers to perform/maintain the selected layout (the MDIParent.Resize, for example).
This method can also be easily adapted to replace the MdiLayout.TileVertical if required.

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)((this.ClientSize.Height / OpenedForms) * HorizontalTileHeightMultiplier);

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

这篇关于如何控制MDI表单中子表单的显示顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆