我曾使用System.Windows.Window.IsActive
来检测窗口是否处于前台,并且在某些情况下它是有效的。但是我发现有些情况下它并不起作用,我想知道是否有其他方法可以检测。
[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;
}