2010-09-16 59 views
2

我有一个时钟应用程序。我已经设置了窗口的TopMost属性。但是,随机地,一些其他窗口或视觉工作室在时钟之上。TopMost不是TopMost总是 - WPF

是否有任何其他方式使我的窗口(时钟应用程序)始终显示在所有其他应用程序之上。

回答

-3

你确定它是一个随机窗口吗?如果另一个窗口也是最上面的窗口,它可能会出现在窗口上方。

+0

呀。我正在寻找的是,其他窗口状态可能是什么,我的时钟应用程序应该在TopMost中。就像“SlinkRun”应用程序一样。 – 2010-09-16 18:09:51

+10

没有SuperDuperTopMost选项。它显然会被所有人使用,从而破坏了它的理由。 – 2010-09-16 18:31:43

+2

@Hans谢谢。但是,可能像SlickRun和其他桌面时钟这样的应用程序如何在所有可能的窗口的最顶层显示自己?有没有其他的“窗口”可以为我提供这种效果? – 2010-09-16 18:45:13

13

我知道这个问题是旧的,但我不太明白为什么接受的答案已经收到了票...或为什么它被接受...它不真的回答这个问题,或提供一个解决方案,并发布这些日子的答案,那几乎总是下调由社区投票和/或删除。好吧,我想这是在不同的时间发布的。

无论哪种方式,尽管它已经很老了,但对于任何未来可能遇到此帖的人来说,我都有一个可能的解决方案。您可以简单地处理Window.Deactivated Event和/或Application.Deactivated Event。当窗口变为背景窗口时,发生Window.Deactivated EventApplication.Deactivated Event当应用程序停止为前台应用程序时发生。

的想法是相关TopMost属性设置为true每个应用程序或Window失去的时间专注:

private void Window_Deactivated(object sender, EventArgs e) 
{ 
    // The Window was deactivated 
    this.TopMost = true; 
} 

值得一提的是,其他开发商也可以使用这种技术,所以这并不能保证你的Window总是仍然是最高的,但它适用于我和情况仍然肯定通过使用它得到改善。

+2

谢谢你写这个 – 2015-10-15 12:27:44

0

我在设置Window.Topmost = true的时候也出现了这个问题,在已经存在的窗口上有时候工作,有时候不行。下面是我的解决方法,如果WS_EX_TOPMOST样式在运行时丢失,则可以将其与其他人提到的Window_Deactivated方法结合使用。

App.Current.MainWindow.Topmost = true; 

// Get this window's handle 
IntPtr hwnd = new WindowInteropHelper(App.Current.MainWindow).Handle; 

// Intentionally do not await the result 
App.Current.Dispatcher.BeginInvoke(new Action(async() => await RetrySetTopMost(hwnd))); 

额外的代码:

private const int RetrySetTopMostDelay = 200; 
private const int RetrySetTopMostMax = 20; 

// The code below will retry several times before giving up. This always worked with one retry in my tests. 
private static async Task RetrySetTopMost(IntPtr hwnd) 
{ 
    for (int i = 0; i < RetrySetTopMostMax; i++) 
    { 
     await Task.Delay(RetrySetTopMostDelay); 
     int winStyle = GetWindowLong(hwnd, GWL_EXSTYLE); 

     if ((winStyle & WS_EX_TOPMOST) != 0) 
     { 
      break; 
     } 

     App.Current.MainWindow.Topmost = false; 
     App.Current.MainWindow.Topmost = true; 
    } 
} 

internal const int GWL_EXSTYLE = -20; 
internal const int WS_EX_TOPMOST = 0x00000008; 

[DllImport("user32.dll")] 
internal static extern int GetWindowLong(IntPtr hwnd, int index);