有没有办法判断我的应用程序是否处于活动状态,即它的任何窗口都有.IsActive = true?
我正在编写Messenger应用程序,并希望它在任务栏中闪烁,当它处于非活动状态并且新消息到达时.
Pom*_*oma 10
使用P/Invoke和循环
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
private static bool IsActive(Window wnd)
{
// workaround for minimization bug
// Managed .IsActive may return wrong value
if (wnd == null) return false;
return GetForegroundWindow() == new WindowInteropHelper(wnd).Handle;
}
public static bool IsApplicationActive()
{
foreach (var wnd in Application.Current.Windows.OfType<Window>())
if (IsActive(wnd)) return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)