最大化MDI子窗体

12
我正在处理一个遗留的WinForms MDI应用程序,并且在使子窗体按照我的意愿行事方面遇到了一些问题。我的目标是让子窗体始终最大化(停靠)。
问题在于,即使我将MaximizeBox设置为false,最大化/调整大小按钮也会出现在MDIs工具栏中,让用户调整大小(取消停靠)子窗体。唯一的方法是将ControlBox设置为false,但那时关闭按钮也会消失(这不是我想要的)。
我已经尝试过使用固定的FormBorderStyle并在触发调整大小事件时最大化子窗体,但我的所有尝试都没有奏效。
我错过了什么超级秘密属性,还是这根本不可能?
最好的问候和提前致谢
//All child forms derive from ChildForm
//Parent MDI Form implementation
//...
private void ShowForm(ChildForm form)
{
    //Check if an instance of the form already exists
    if (Forms.Any(x => x.GetType() == form.GetType()))
    {
        var f = Forms.First(x => x.GetType() == form.GetType());
        f.Focus();
        f.WindowState = FormWindowState.Maximized;
    }
    else
    {
        //Set the necessary properties (any other properties are set to default values)
        form.MdiParent = this;
        form.MaximizeBox = false;
        form.MinimizeBox = false;
        form.WindowState = FormWindowState.Maximized;
        Forms.Add(form);
        form.Forms = Forms;
        form.Show();
        form.Focus();
        //Lets make it nasty (some forms aren't rendered properly otherwise)
        form.WindowState = FormWindowState.Normal;
        form.WindowState = FormWindowState.Maximized;
    }
}
//...

//ChildForm implementation
//...
public List<Form> Forms { get; set; }
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    Forms.RemoveAll(x => x.GetType() == GetType());
}

protected override void OnResize(EventArgs e)
{
    WindowState = FormWindowState.Maximized;
}

2
当您希望子窗体始终最大化时,使用MDI没有意义。无论如何,当您切换窗口时,MDI都会与您作斗争,并出现大量闪烁。只需使用用户控件并将其交换到表单中即可。 - Hans Passant
1
你可能是对的,但正如我所说,这是遗留代码,我不想过多地修改它。 - Jay
5个回答

22

这个问题不容易解决,但我意外地发现答案并且相当简单:默认情况下将子窗体的窗口状态设置为 Normal。然后确保在调用 Show() 方法之后重置子窗口的窗口状态

示例:

private void ShowNewForm(object sender, EventArgs e)
{
  Form childForm = new Form();
  childForm.MdiParent = this;
  childForm.Text = "Window " + childFormNumber++;
  childForm.Show();
  childForm.WindowState = FormWindowState.Maximized;
}

16

你可以覆盖每个你想确保不会最小化的子窗体的OnResize事件。或者创建一个BaseForm并让所有子窗体都继承自它。

protected override void OnResize(EventArgs e)
{
   this.WindowState = FormWindowState.Maximized;
}

此外,您可以使用X、Y坐标,但是OnResize应该足够了。将此代码放入子窗体的构造函数中:

   this.WindowState = FormWindowState.Maximized;

   Point NewLoc = Screen.FromControl(this).WorkingArea.Location;
   //Modifiy the location so any toolbars & taskbar can be easily accessed.
   NewLoc.X += 1;
   NewLoc.Y += 1;
   this.Location = NewLoc;

   Size NewSize = Screen.FromControl(this).WorkingArea.Size;
   //Modifiy the size so any toolbars & taskbar can be easily accessed.
   NewSize.Height -= 1;
   NewSize.Width -= 1;
   this.Size = NewSize;

   this.MinimumSize = this.Size;
   this.MaximumSize = this.MinimumSize;

我从这里获取了X,Y的代码: http://bytes.com/topic/c-sharp/answers/278649-how-do-i-prevent-form-resizing


5
form1 obj = new form1 ();
obj.MdiParent = MDIGracular.ActiveForm;
obj.StartPosition = FormStartPosition.CenterParent;
obj.WindowState = FormWindowState.Minimized;
obj.Dock = DockStyle.Fill;
obj.Show();
obj.WindowState = FormWindowState.Maximized;

3

这是我如何克服相同的问题,不记得在哪里找到代码了。

private const int WM_SYSCOMMAND = 0x112;
private const int SC_MINIMIZE = 0xF020;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_CLOSE = 0xF060;
private const int SC_RESTORE = 0xF120;

protected override void WndProc(ref Message msg)
{
  if ((msg.Msg == WM_SYSCOMMAND) && 
    (((int)msg.WParam == SC_MINIMIZE) || ((int)msg.WParam == SC_MAXIMIZE) ||
    ((int)msg.WParam == SC_CLOSE)) || ((int)msg.WParam == SC_RESTORE))
    {
      //do nothing
    } // end if
    else
    {
      base.WndProc(ref msg);
     } // end else
}

1
在我的应用程序中,我发现如果我在表单加载事件中只放置这两行代码,它就可以工作。感谢sarvjeet提供的基本思路。+1给你。
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接