11

我正在开发旧版 WinForms MDI 应用程序,并且在使子表单按我想要的方式运行时遇到了一些麻烦。我的目标是让子窗体始终最大化(停靠)。

问题是,即使我设置MaximizeBoxfalse最大化/调整大小按钮出现在 MDI 工具条中并让用户调整(取消停靠)子窗体的大小。避免这种情况的唯一方法是设置ControlBoxfalse,然后关闭按钮消失(这不是我想要的)。

我已经尝试在FormBorderStyle触发调整大小事件时使用固定并最大化子表单,但我的方法都没有奏效。

有没有我错过的超级秘密财产,或者这是不可能的?

提前致以最诚挚的问候和感谢

更新

我写了一个低俗的方法(感谢@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;
}
4

5 回答 5

20

这个问题并不容易解决,但我无意中找到了答案,而且很简单;默认设置子窗体的windowstate为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;
}
于 2012-10-02T14:15:24.923 回答
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

于 2012-01-06T22:36:33.287 回答
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;
于 2016-10-14T13:19:04.587 回答
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
}
于 2012-09-24T13:55:11.790 回答
0

在我的应用程序中,我发现如果我只将这两行放在表单加载事件中,它就可以工作。感谢 sarvjeet 的基本想法。为你 +1

this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;
于 2017-09-01T16:22:43.667 回答