C#Winforms主要表单发送回来

时间:2017-11-27 17:38:06

标签: c# winforms overlay bringtofront

我的Winforms应用有一个MainForm和一个MainWaitForm。出于某种原因,我不明白,我的MainForm一直被发送到其他打开的窗口后面,主要是当用户在显示MainWaitForm时点击任何内容时...例如,运行应用时在VisualStudio调试器模式下,等待对话框显示,但VisualStudio紧随其后,而不是将MainForm放在第二级。

以下是等待表单覆盖的代码:

static Control _parent;
public static void ShowWaitForm(Control parent) {
    _parent = parent;
    // some attempt to bring the active view to front first...
    if (_parent != null && _parent.Visible) {
      _parent.BringToFront();
    }
    else {
      MyMainForm.BringToFront();
    }
    // Make sure it is only launched once.
    if (_instance != null) {
      return;
    }
    _thread = new Thread(new ParameterizedThreadStart(MainWaitForm.ShowForm));
    _thread.IsBackground = true;
    _thread.SetApartmentState(ApartmentState.STA);
    _thread.Start(parent);
    while (_instance == null || _instance.IsHandleCreated == false) {
    System.Threading.Thread.Sleep(100);
    }
}

// the hide method
 public static void HideWaitForm() {
  // again, attempt to bring form to front...
  if (_parent != null && _parent.Visible) {
      _parent.BringToFront();
  }
  else {
      MyMainForm.BringToFront();
  }
  if (_instance != null) {
    if (_instance.InvokeRequired) {
      _instance.Invoke(new System.Windows.Forms.MethodInvoker(delegate {
        CloseForm();
      }));
    }
    else {
      CloseForm();
    }
  }
}

static void CloseForm() {
  _instance.Close();
  _thread = null;
  _instance = null;
}

0 个答案:

没有答案