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

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


只有父表单可以选择。子窗体显示在窗口/任务栏中,但似乎被窗口“锁定”,无法最大化或更改为常规样式。
我该如何解决?

最佳答案

尝试在form.WindowStateShow()方法调用之后设置Hide()

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

10-08 06:41