c# - 如何在父窗口最小化时防止子窗口最小化

标签 c# .net windows vb.net winforms

如果我使用 .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/

相关文章:

c - C 中文本文件的奇怪错误

javascript - 如何在 cshtml 页面中将选项传递给 JavaScript

将数据加载到 SQL Server 时的 C# 多线程问题

python - 如何查看文件的更改?

.net - iFrame 中的第 3 方应用程序必须保持标题/顶部框架 session 处于事件状态

.net - 保持 sql 数据库模式在开发人员之间同步

c++ - 将应用程序部署为静态或动态构建的最佳实践是什么?

c# - 异常处理 - catch 子句中的异常?

c# - Ninject 和异步操作

c# - Validator.TryValidateProperty 不工作