如果我使用 .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);
}
}
关于c# - 如何在父窗口最小化时防止子窗口最小化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8563803/