C#恢复Form的大小

时间:2016-02-24 09:16:58

标签: c# forms winforms resize mdichild

我为MDI Child ovveride举办了onResize活动Form,这是代码:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    if (WindowState == FormWindowState.Maximized)
    {
        WindowState = FormWindowState.Normal;
        Height = MdiParent.ClientSize.Height - 70;
        Width = MdiParent.ClientSize.Width - 10;
        Location = new Point(0, 0);
    }
}

当用户点击Maximize上的Form按钮时,我会设置"标准"尺寸。 所以它看起来像这样......

Resize of the Form

如何将按钮Maximize更改为Restore,因为它已达到我设定的最大尺寸?

1 个答案:

答案 0 :(得分:1)

有三种方法可以实现响应式Windows应用程序:

  1. 尝试使用拆分容器面板(适用于信息,MSDN Info

  2. 使用Anchor(YouTube link

  3. 使用以下代码

                private Size oldSize;
    
                protected override void OnResize(System.EventArgs e)
                {
                    base.OnResize(e);
                    foreach (Control cnt in this.Controls)
                    {
                        ResizeAll(cnt, base.Size);
                    }
    
                    oldSize = base.Size;
                }
    
                private void ResizeAll(Control cnt, Size newSize)
                {
                    int iWidth = newSize.Width - oldSize.Width;
    
                    cnt.Left += (cnt.Left * iWidth) / oldSize.Width;
                    cnt.Width += (cnt.Width * iWidth) / oldSize.Width;            
                    int iHeight = newSize.Height - oldSize.Height;
                    cnt.Top += (cnt.Top * iHeight) / oldSize.Height;
                    cnt.Height += (cnt.Height * iHeight) / oldSize.Height;
                }
    
  4. 在windows form-resize事件中调用此事件。