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