最大化 MDI 子窗体

发布于 2024-12-25 03:21:21 字数 1717 浏览 8 评论 0原文

我正在开发一个遗留的 WinForms MDI 应用程序,并且在使子窗体按我想要的方式运行时遇到一些问题。 我的目标是让子窗体始终最大化(停靠)。

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

我已经尝试使用固定的 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;
}

I'm working on a legacy WinForms MDI application and have some trouble making the child forms behave as I want.
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.
The only way to avoid this is to set ControlBox to false but then the close button disappears to (thats not what I want).

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.

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最单纯的乌龟 2025-01-01 03:21:21

这个问题并不好解决,但是我无意中找到了答案,而且很简单;默认情况下,将子窗体的窗口状态设置为“正常”。然后确保在调用 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;
}

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. Then make sure that you reset the windowstate of the child window AFTER you call the Show() method.

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

您可以覆盖要确保不会最小化的每个子窗体的 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/主题/c-sharp/answers/278649-how-do-i-prevent-form-resizing

You can override the OnResize of each child form you want to make sure does not minimize. Or create a BaseForm and inherit all children forms from it.

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

Also, you can use X,y coordinates, but OnResize should be enough. 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

一杆小烟枪 2025-01-01 03:21:21
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;
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;
谁人与我共长歌 2025-01-01 03:21:21

这就是我克服同样问题的方法,不记得在哪里找到代码了。

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
}

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
}
瞳孔里扚悲伤 2025-01-01 03:21:21

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

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

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. +1 for you

this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文