如何确定我的应用程序是否处于活动状态(拥有焦点) [英] How to determine whether my application is active (has focus)

查看:257
本文介绍了如何确定我的应用程序是否处于活动状态(拥有焦点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉我的应用程序是否处于活动状态,即任何一件事的窗户已经.IsActive =真的吗?

Is there a way to tell whether my application is active i.e. any of it's windows has .IsActive=true?

我写的信使应用程序,并希望它在任务栏闪烁当它不活跃,新邮件到达。

I'm writing messenger app and want it to flash in taskbar when it is inactive and new message arrives.

推荐答案

用于P /调用和循环

[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;
}

这篇关于如何确定我的应用程序是否处于活动状态(拥有焦点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆