如果我使用.Show(frmParent)显示一个新的非模式子窗口,然后父窗口最小化,那么孩子也会自动最小化。
防止这种情况的最好方法是什么?
编辑:子窗口必须是非模态的 ,它必须有父集 。
它被称为“拥有的窗口”,而不是子窗口。 Windows确保拥有的窗口始终位于其所有者之上。 这意味着当业主最小化时,它必须被最小化。
Winforms不支持动态更改所有者。 这个示例代码运行良好:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Form ownedWindow; private void button1_Click(object sender, EventArgs e) { if (ownedWindow != null) return; ownedWindow = new Form2(); ownedWindow.FormClosed += delegate { ownedWindow = null; }; ownedWindow.Show(this); } protected override void WndProc(ref Message m) { // Trap the minimize and restore commands if (m.Msg == 0x0112 && ownedWindow != null) { if (m.WParam.ToInt32() == 0xf020) ownedWindow.Owner = null; if (m.WParam.ToInt32() == 0xf120) { ownedWindow.Owner = this; ownedWindow.WindowState = FormWindowState.Normal; } } base.WndProc(ref m); } }
如果子窗口的行为应该像对话框一样(只要打开窗口就不能与父窗口交互),则调用.ShowDialog(frmParent)
。