问题
I have a Winforms application that uses show multiple top-level windows:
Form1 form1 = new Form1();
form1.Show();
Form2 form2 = new Form2();
form2.Show();
Application.Run();
Inside one of the event-handlers in Form1, I would like to be able to show a modal dialog:
Dialog dialog = new Dialog();
dialog.ShowDialog(form1);
without suspending the other top-level window.
Is this possible?
回答1:
You'd need to run each top-level window on its own STA thread to achieve that, I believe.
回答2:
There is a simple solution that seems to work properly. You can simply check if we are being disabled and re-enable if needed.
[DllImport("user32.dll")]
private static extern void EnableWindow(IntPtr handle, bool enable);
protected override void WndProc(ref System.Windows.Forms.Message msg)
{
if (msg.Msg == 0x000a /* WM_ENABLE */ && msg.WParam == IntPtr.Zero)
{
EnableWindow(this.Handle, true);
return;
}
base.WndProc(ref msg);
}
回答3:
If you need an alternate method to running multiple UI threads, you can handle the WM_ENABLE message and use the EnableWindow method to prevent the Form from being disabled.
回答4:
Once you show a modal dialog, it will make all other windows on the same STA thread unusable. The reason behind this is the modal dialog will start intercepting all messages for that particular thread. The other top level windows will not be able to respond until the modal dialog is closed.
来源:https://stackoverflow.com/questions/581528/c-sharp-winforms-multiple-top-level-windows-and-showdialog