C#Topmost = true-限于应用程序

时间:2018-11-20 21:31:23

标签: c# winforms

如果有新版本可用,我们的应用程序将显示“更新提示”。

此更新提示应位于应用程序的“最高”位置,但是,如果将应用程序最小化或发送到后台,则更新提示也应消失。

只需使用

this.TopMost = true;

将覆盖当前正在运行的“ ANY”应用程序...

有没有办法仅“叠加”当前应用程序生成的窗口?


所需: 当应用程序位于前台时,应用程序会在每个窗口的顶部显示更新提示。切换到另一个应用程序还将把更新提示发送到后台。


所需:Update-Hint覆盖当前应用程序的任何窗口:

enter image description here

不需要:Update-Hint也可以覆盖FOREIGN应用程序:

enter image description here

2 个答案:

答案 0 :(得分:4)

尽管拥有该属性的名称,但TopMost实际上是您在这里的敌人。要使“浮动表单”停留在主要表单之上,而又不会在其他应用程序获得焦点时使其模糊不清,请尝试以下方法:

FormX f = new FormX();
f.Show(this);

在此示例中,“ this”是主要的表单实例。这意味着您创建的表单现在由主表单拥有,并将使其浮于其上。您会获得额外的好处,即在最小化主窗体时,浮动窗体也将消失。

答案 1 :(得分:0)

我想出了一种解决方法。

需要设置owner中的UpdateHint,但是要使其保持在每个应用程序窗口的顶部,如果显示或激活了一个新窗口,则必须更改所有者。 。

在我们的应用程序中,每个Form都继承InterceptorForm,所以我要做的就是相应地修改InterceptorForm

将所有者更改为this,除非没有对话框,或者对话框本身为this

public class InterceptorForm : Form
{
    protected override void OnLoad(EventArgs e)
    {
        ...

        if (this.GetType() != typeof(UpdateHint) && MainWindow.updateHint != null)
        {
            Log.t("Setting owner on update hint during onload: " + this.GetType());
            MainWindow.updateHint.Owner = this;
        }

        base.OnLoad(e);
    }

    protected override void OnActivated(EventArgs e)
    {
        if (this.GetType() != typeof(UpdateHint) && MainWindow.updateHint != null)
        {
            Log.t("Setting owner on update hint: " + this.GetType());
            MainWindow.updateHint.Owner = this;
        }

        base.OnActivated(e);
    }
}

UpdateHint现在位于属于我们应用程序的每个窗口的顶部,但是可以被任何其他应用程序覆盖。

相关问题