I used System.Windows.Window.IsActive
to detect whether the window is in the foreground and it worked in some cases. But I have found cases where it doesn't and I wonder if there is any way to detect it开发者_C百科.
The method below works except when a background-only process gets the focus. This is the case with the Windows Desktop. It steals the status of foreground window but it isn't in the foreground.
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public bool IsForeground()
{
Window window = Application.Current.MainWindow;
IntPtr windowHandle = new WindowInteropHelper(window).Handle;
IntPtr foregroundWindow = GetForegroundWindow();
return windowHandle == foregroundWindow;
}
精彩评论