最大化拥有的表单未正确恢复 [英] Maximized owned Form not restoring correctly

查看:27
本文介绍了最大化拥有的表单未正确恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个按钮,可以打开一个新表单作为拥有的表单.(很简单,除了下面没有别的逻辑)

I have a button on a form which opens a new form as an owned form. (It's very simple, no other logic than below)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form form = new Form();
        form.Show(this);
    }
}

我的问题如下:

  1. 如果我单击按钮以获取一个拥有的表单的实例并将其拖到它自己的监视器上.
  2. 最大化拥有的表单
  3. 最小化原始主窗体 (Form1)
  4. 恢复原来的主表单(Form1)

然后在恢复时最大化拥有的表单不再最大化而是具有正常状态.

Then on restore the maximized owned form is no longer maximized but has a state of Normal.

拥有的表单被设计为一个工具窗口,所以我不能打破所有者/拥有的关系.这似乎是 winforms 的问题,但我知道应该可以纠正,因为 VS 行为正确并将窗口恢复到最大化而不是正常.

The Owned form is styled as a tool window so I cannot break the Owner/Owned relationship. It appears to be a thing with winforms but I know it should be possible to correct since VS behaviors correctly and restores the window to Maximized rather than to Normal.

推荐答案

这是一种可能...

向拥有的表单添加一个属性以跟踪其最后一个FormWindowState(如果您不想公开它,可以只是private):

Add a property to the Owned form to track its last FormWindowState (could just be private if you don't care to expose it):

private FormWindowState _lastState;
public FormWindowState LastWindowState { get { return _lastState; } }

WndProc 的覆盖添加到拥有的表单中:

Add an override for WndProc to the Owned form:

protected override void WndProc(ref Message message)
{
    const Int32 WM_SYSCOMMAND = 0x0112;
    const Int32 SC_MAXIMIZE = 0xF030;
    const Int32 SC_MINIMIZE = 0xF020;
    const Int32 SC_RESTORE = 0xF120;

    switch (message.Msg)
    {
    case WM_SYSCOMMAND:
        {
        Int32 command = message.WParam.ToInt32() & 0xfff0;
        switch (command)
        {
            case SC_MAXIMIZE:
            _lastState = FormWindowState.Maximized;
            break;
            case SC_MINIMIZE:
            _lastState = FormWindowState.Minimized;
            break;
            case SC_RESTORE:
            _lastState = FormWindowState.Normal;
            break;
        }
        }
        break;
    }

    base.WndProc(ref message);
}

最后,为 Ownered 表单的 VisibleChanged 事件添加一个处理程序:

Finally, add a handler for the Owned form's VisibleChanged event:

private void Form2_VisibleChanged(object sender, EventArgs e)
{
    WindowState = _lastState;
}

这篇关于最大化拥有的表单未正确恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆