[英]C# Topmost=true - restrict to application
尽管属性名称,TopMost实际上是您在这里的敌人。 要使“浮动表单”停留在主表单之上,而又不会在其他应用程序获得焦点时使其模糊,请尝试这种方式:
FormX f = new FormX();
f.Show(this);
在此示例中,“ this”是主要表单实例。 这意味着您创建的表单现在由主表单拥有,并将使其浮于其上。 您获得了额外的好处,即当最小化主窗体时,浮动窗体也将消失。
我想出了一种解决方法。
需要设置UpdateHint
的owner
,但是要使其保持在每个应用程序窗口的顶部,如果显示或激活了一个新的Window,则必须更改所有者。
在我们的应用程序中,每个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
停留在属于我们应用程序的每个窗口的顶部,但是可以被任何其他应用程序覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.