c# - 获取当前窗口标题

时间:2014-11-06 16:22:09

标签: c# title

我正在写一个键盘记录程序。在我的程序中,我想写入活动的日志文件当前窗口标题。如果用户更改为其他窗口,日志文件将附加新的标题窗口。这里我有获取Windows标题的代码:

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern int GetWindowText(IntPtr hwnd, string lpString, int cch);
    public static string ActiveApplTitle()
    {
        //This method is used to get active application's title using GetWindowText() method present in user32.dll
        IntPtr hwnd = GetForegroundWindow();
        if (hwnd.Equals(IntPtr.Zero)) return "";
        string lpText = new string((char)0, 100);
        int intLength = GetWindowText(hwnd, lpText, lpText.Length);
        if ((intLength <= 0) || (intLength > lpText.Length)) return "unknown";
        return lpText.Trim();
    }

所以,我不知道在有变化时如何更新窗口标题。请给我一个主意。非常感谢!

1 个答案:

答案 0 :(得分:-2)

你能把它放在另一个线程的循环中吗?有两个变量:previousWindow和currentWindow。继续比较它们,当它们发生变化时 - 您更新日志文件。

相关问题