为什么从.hide()变为.show()的表单不能最大化呢?

本文关键字:不能 最大化 表单 show hide 变为 为什么 | 更新日期: 2023-09-27 18:19:00

我编写了一个方法来隐藏或显示应用程序的所有窗体(包括窗体的子窗体)。代码如下:

public enum FormState
{
    Show ,
    Hidden,
    Enable,
    Disable
}
private void SetAllFormsState(FormState formState)
{
    FormCollection forms = Application.OpenForms;
    FormWindowState formWinState;
    bool state;
    if (formState == FormState.Show)
    {
        formWinState = FormWindowState.Normal;
        state = true;
    }
    else if (formState == FormState.Hidden)
    {
        formWinState = FormWindowState.Minimized;
        state = false;
    }
    else
    {
        throw new ArgumentNullException("invalid flag");
    }
    for (int i = forms.Count - 1; i >= 0; i--)
    {
        Form form = forms[i];
        form.WindowState = formWinState;
        if (state)
        {
            form.Show();
        }
        else
        {
            form.Hide();
        }
    }
}

但是当我再次调用形式:

SetAllFormsState(FormState.Show);

只能选择父表单。子窗体显示在窗口/任务栏中,但似乎被窗口"锁定",无法最大化或更改为正常样式。我怎么修理它?

为什么从.hide()变为.show()的表单不能最大化呢?

尝试将form.WindowState 设置在Show()Hide()方法调用之后:

for (int i = forms.Count - 1; i >= 0; i--)
{
  Form form = forms[i];
  if (state)
  {
    form.Show();
  }
  else
  {
    form.Hide();
  }
  form.WindowState = formWinState;
}

不妨试试:

private void SetAllFormsState(FormState formState) 
{
    FormCollection forms = Application.OpenForms;
    FormWindowState formWinState;
    bool state;
    switch(formState)
    {
        case FormState.Show:
            formWinState = FormWindowState.Normal;
            state = true;
            break;
        case FormState.Hidden:
            formWinState = FormWindowState.Minimized;
            state = false;
            break;
        default:
            throw new ArgumentNullException("invalid flag");
            break;
    }
    foreach(Form form in forms)
    {
        form.WindowState = formWinState;
        if (state)
            form.Show();
        else
            form.Hide();
    }
}