简体   繁体   English

最大化MDI子表单

[英]Maximize MDI child form

I'm working on a legacy WinForms MDI application and have some trouble making the child forms behave as I want. 我正在研究一个传统的WinForms MDI应用程序,并且在使子表单按我的意愿运行时遇到一些麻烦。 My objective is to have the child form always maximized (docked). 我的目标是让孩子的形式始终最大化(停靠)。

The problem is, that even if I set MaximizeBox to false the maximize/resize button appears in the MDIs toolstrip and let the user resize (undock) the child form. 问题是,即使我将MaximizeBox设置为false ,MDIs工具条中也会出现最大化/调整大小按钮,并让用户调整(取消停靠)子窗体。 The only way to avoid this is to set ControlBox to false but then the close button disappears to (thats not what I want). 避免这种情况的唯一方法是将ControlBox设置为false ,然后关闭按钮消失(这不是我想要的)。

I've already tried to use a fixed FormBorderStyle and to maximize the child form when the resize event is fired but none of my approaches worked. 我已经尝试使用固定的FormBorderStyle并在触发resize事件时最大化子窗体,但我的方法都不起作用。

Is there any super secret property I have missed or is it just impossible? 我错过了什么超级秘密财产,还是不可能?

Best Regards & Thanks in advance 最好的问候和提前谢谢

Update 更新

I wrote a sleazy method (thanks to @rfresia) for handling my child form, it may help others who run into the same issue: 我写了一个低级方法(感谢@rfresia)来处理我的子表单,它可能会帮助其他遇到同样问题的人:

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

The problem wasn't easy to solve, but I accidentally found the answer and it's quite simple; 这个问题不容易解决,但我偶然发现了答案,而且很简单; set the windowstate of the child form to Normal by default. 默认情况下,将子窗体的windowstate设置为Normal。 Then make sure that you reset the windowstate of the child window AFTER you call the Show() method. 然后确保在调用Show()方法重置子窗口的windowstate。

Example: 例:

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

You can override the OnResize of each child form you want to make sure does not minimize. 您可以覆盖要确保不会最小化的每个子表单的OnResize。 Or create a BaseForm and inherit all children forms from it. 或者创建一个BaseForm并从中继承所有子表单。

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

Also, you can use X,y coordinates, but OnResize should be enough. 此外,您可以使用X,y坐标,但OnResize应该足够了。 Put this in the child form constructor: 把它放在子表单构造函数中:

   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;

I got the code for the X,Y from here: http://bytes.com/topic/c-sharp/answers/278649-how-do-i-prevent-form-resizing 我从这里得到了X,Y的代码: http//bytes.com/topic/c-sharp/answers/278649-how-do-i-prevent-form-resizing

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;

This is how I overcame the same promblem, cannot remember where I found the code. 这就是我如何克服同样的问题,不记得我在哪里找到了代码。

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
}

In my app I found if I put just these two lines in the form loaded event it worked. 在我的应用程序中,我发现如果我只将这两行放在它运行的表单加载事件中。 Thanks sarvjeet for the basic idea. 感谢sarvjeet的基本想法。 +1 for you 为你+1

this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;

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

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