Reputation: 264
If I show a new non-modal child window using .Show(frmParent), and then the parent window is minimized, the child will get minimized automatically as well.
What is the best way to prevent this?
EDIT: The child window must be non-modal, and it must have the parent set.
Upvotes: 3
Views: 5077
Reputation: 941465
It is called "owned window", not child window. Windows ensures that the owned window is always on top of its owner. Which implies it has to be minimized when the owner is minimized.
Winforms does however support changing the owner on the fly. This sample code worked well:
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);
}
}
Upvotes: 6
Reputation: 12458
If the child window should behave like a dialog (you cannot interact with the parent window as long as it is open) then call .ShowDialog(frmParent)
.
Upvotes: 0