如何获取用户当前关注的窗口标题? 我正在制作一个与另一个窗口一起运行的程序,如果用户没有关注那个窗口,我发现我的程序没有理由继续更新。
那么我怎样才能确定用户关注的是哪个窗口呢?
我确实尝试调查过
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
但我似乎只能在 Window 是我的应用程序的一部分时才能使用它,但事实并非如此。
最佳答案
检查这段代码:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
关于c# - 如何获取不属于我的应用程序的事件窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32965310/