WPF永远在顶部

Kub*_*zyk 45 c# wpf topmost

即使其他应用程序在Fullscreen上运行,是否可以使窗口始终保持在顶部?我正在使用TopMost = true,但当其他应用程序在全屏运行时,我的隐形.WindowStyle = None顺便说一句,这是窗口.

编辑:不要让其他窗口最小化

She*_*dan 44

这不会在100%的时间内起作用,但它会在某种程度上改善这种情况.您可以Topmost = trueWindow.Deactivated事件的处理程序中设置:

private void Window_Deactivated(object sender, EventArgs e)
{
    Window window = (Window)sender;
    window.Topmost = true;
}
Run Code Online (Sandbox Code Playgroud)

Deactivated只要您的应用程序失去焦点(通常在另一个应用程序请求时Topmost),就会调用该事件,因此这将在此之后重置您的应用程序.

  • @Sheridan 根据您所说的,该事件将在失去焦点时被调用。因此,如果多个应用程序尝试执行此操作,这是否是一个潜在的无限循环? (4认同)
  • 也可以在 XAML 中设置: <Window ... Topmost="True"> (3认同)
  • 不能按需工作,但谢谢。 (2认同)

Moh*_*dil 12

从MSDN尝试此解决方案,它应该适合您.在Window Activated Event添加以下代码:

this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.Topmost = true;
this.Top = 0;
this.Left=0;
Run Code Online (Sandbox Code Playgroud)

DeActivated Event添加以下代码

this.Topmost = true;
this.Activate();
Run Code Online (Sandbox Code Playgroud)

来自MSDN的原帖


Tra*_*bbs 6

上面对我来说没有一个解决方案有效,所以这就是我最终要做的。它对我来说非常有效。

基本上,要使其保持在顶部,您只需设置失去焦点事件以使其回到顶部。

XAML:

PreviewLostKeyboardFocus="Window_PreviewLostKeyboardFocus"
Run Code Online (Sandbox Code Playgroud)

背后的代码:

private void Window_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
     {
          Window window = (Window)sender;
          window.Topmost = true;
     }
Run Code Online (Sandbox Code Playgroud)