如何获得不属于我的应用程序的活动窗口? [英] How to get active window that is not part of my application?
本文介绍了如何获得不属于我的应用程序的活动窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取用户当前关注的窗口标题? 我正在制作一个与另一个Window一起运行的程序,如果用户没有将注意力集中在该窗口上,则我的程序没有理由继续更新.
How can I get the Window Title that the user currently have focus on? I'm making a program that runs with another Window, and if the user does not have focus on that window I find no reason for my program to keep updating.
那么如何确定用户关注的窗口?
So how can I determine what window the user have focus on?
我确实尝试研究了
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
但是我似乎只能在Window是我的应用程序的一部分的情况下使用它,而事实并非如此.
but I seems I can only use that if the Window is part of my application which is it not.
推荐答案
检查此代码:
[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;
}
这篇关于如何获得不属于我的应用程序的活动窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文