简体   繁体   English

C#WinForms:如何限制MDI子窗口始终位于MDIParent的范围内?

[英]C# WinForms: How to restrict MDI child windows to always be within the bounds of the MDIParent?

I have a C# WinForms MDI application that has a few MDI child windows. 我有一个具有一些MDI子窗口的C#WinForms MDI应用程序。 Users are able to move or resize the individual windows. 用户可以移动或调整各个窗口的大小。 The issue is that when they move the windows to the bounds of the MDI parent, scrollbars appear on the MDIParent window and the user can drag the child windows outside the bounds of the MDI parent. 问题是,当他们将窗口移到MDI父级的边界时,滚动条出现在MDIParent窗口上,用户可以将子窗口拖动到MDI父级的边界之外。 Is there any way to change this behavior so that the child windows are always inside the parent window and no scrollbars are ever created? 有什么方法可以更改此行为,以使子窗口始终位于父窗口中,并且永远不会创建滚动条? I know there are ways to "pop" the child windows back inside the parent window by overriding the OnMove event. 我知道可以通过重写OnMove事件将子窗口“弹出”回到父窗口中。 I want the windows to stay inside even when the user is moving the window. 我希望即使用户在移动窗口,窗口也要留在里面。 Is there any way to do this? 有什么办法吗?

On your child forms, handle the FormResize event, with something like this: 在您的子窗体上,使用以下形式处理FormResize事件:

private void Form1_Resize(object sender, EventArgs e)
{
    Size pSize = this.ParentForm.ClientSize;

    Size maxAllowed = new Size(pSize.Width - this.Left, pSize.Height - this.Top);

    // Resize the child if it goes out of bounds
    if (this.Height > maxAllowed.Height)
        this.Height = maxAllowed.Height;

    if (this.Width > maxAllowed.Width)
        this.Width = maxAllowed.Width;
}

probably not great results depending on wht you want. 取决于您想要的结果,可能效果不是很好。

I would probably redo it so that it moves te form back in bounds instead of resizing it. 我可能会重做它,以使它向后移动而不是调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM