首页 > 解决方案 > 即使在 WinForms 中停用后,如何将子窗体保留在屏幕上?

问题描述

我的主窗体中有一个按钮。按下时,将出现子窗体。子表单充当通知。

通常,当任何(任何应用程序的)窗口打开时,如果我们单击另一个应用程序,打开的窗口将被停用并位于应用程序(我们单击的)后面。

但是当按下某些东西时(任何应用程序的)通知不会离开屏幕(或落后)。我想要那样的东西。

例子

Windows 中的“放大镜”应用程序在“放大镜”应用程序打开时单击另一个应用程序时不会出现在屏幕后面。

我想对子表单进行编码,以便即使在停用后它也不会离开屏幕。

我的代码(打开子表单)-

 private void button2_Click(object sender, EventArgs e) {
            this.Visible = false; // to make the main form invisible

            for (int x = 0; x <= x++; x++) { //infinite loop
                notificationForm n = new();
                n.StartPosition = FormStartPosition.Manual;
                n.Location = new Point(0, 0);
                n.ShowDialog();

                Task.Delay(3000).Wait(); // I made a for loop and included this line of code to open the notification(child form) every 3 seconds after closing it
            }
        }

标签: c#winforms

解决方案


尝试将表单的 TopMost 属性设置为 true,如下所示:

private void button2_Click(object sender, EventArgs e) {
        this.Visible = false; // to make the main form invisible

        for (int x = 0; x <= x++; x++) { //infinite loop
            notificationForm n = new();
            n.StartPosition = FormStartPosition.Manual;
            n.Location = new Point(0, 0);
            n.TopMost = true;
            n.ShowDialog();

            Task.Delay(3000).Wait(); // I made a for loop and included this line of code to open the notification(child form) every 3 seconds after closing it
        }
    }

推荐阅读