如何获取用户当前所关注的窗口标题? 我正在制作一个与另一个窗口一起运行的程序,如果用户没有关注该窗口,我发现没有理由让我的程序继续更新。如何获得不属于我的应用程序的活动窗口?
那么如何确定用户关注的窗口?
我曾尝试寻找到
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
,但我似乎我只可以使用,如果窗口是我的应用程序的一部分,这又何尝不是。
如何获取用户当前所关注的窗口标题? 我正在制作一个与另一个窗口一起运行的程序,如果用户没有关注该窗口,我发现没有理由让我的程序继续更新。如何获得不属于我的应用程序的活动窗口?
那么如何确定用户关注的窗口?
我曾尝试寻找到
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
,但我似乎我只可以使用,如果窗口是我的应用程序的一部分,这又何尝不是。
检查这个代码:
[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;
}
现在就工作,谢谢。我可以接受它。 –
抱歉,花了这么长时间才接受答案,现在就完成了。 –
@JoakimCarlsson多数民众赞成!谢谢 :) –
使用GetForegroundWindow来检索聚焦窗口的句柄和GetWindowText以获得窗口标题。
[ DllImport("user32.dll") ]
static extern int GetForegroundWindow();
[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);
static void Main() {
StringBuilder builder = new StringBuilder(255) ;
GetWindowText(GetForegroundWindow(), builder, 255) ;
Console.WriteLine(builder) ;
}
由于某种原因,这将是建设者的长度将为0. –
你读过该函数的文档?如果是这样,哪一部分不清楚?如果不是,为什么不呢? –