停止表单退出父表单

Stop form from exiting parent form

我不是要为我编写代码,而是要一条路径。

我正在寻找一种方法来防止打开的表单离开父表单的边界。

Like keep both forms open, but not allow another form that is opened via the program to leave the bounds of the other program. Best example is like an operating system.

我的意思是图片:

谢谢,有问题请追问! 奥斯汀

MDI 解决方案是一个起点,但子 MDI 窗体仍然可以移到父 MDI 窗体的可见范围之外 window。要解决此问题,您需要向子 MDI 窗体添加事件处理程序,以便在每个子 window 移动后,它仍然包含在父 MDI 窗体中。

下面的示例代码来自 MSDN 论坛上一个非常古老的问题,但仍然很有魅力 :) 来源:https://social.msdn.microsoft.com/Forums/windows/en-US/46e35e80-7bfa-447a-9655-965134124f70/prevent-child-form-from-leaving-parent-form-bounds?forum=winforms

protected override void OnMove(EventArgs e)
{
    //
    // Get the MDI Client window reference
    //
    MdiClient mdiClient = null;
    foreach(Control ctl in MdiParent.Controls)
    {
        mdiClient = ctl as MdiClient;
        if(mdiClient != null)
            break;
    }
    //
    // Don't allow moving form outside of MDI client bounds
    //
    if(Left < mdiClient.ClientRectangle.Left)
        Left = mdiClient.ClientRectangle.Left;
    if(Top < mdiClient.ClientRectangle.Top)
        Top = mdiClient.ClientRectangle.Top;
    if(Top + Height > mdiClient.ClientRectangle.Height)
        Top = mdiClient.ClientRectangle.Height - Height;
    if(Left + Width > mdiClient.ClientRectangle.Width)
        Left = mdiClient.ClientRectangle.Width - Width;
    base.OnMove(e);
}