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