【问题标题】:c# - Get current window title [duplicate]c# - 获取当前窗口标题
【发布时间】:2014-11-06 16:22:09
【问题描述】:

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

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

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

【问题讨论】:

  • I'm writing a keylogger ---> 祝你在这里获得帮助
  • 您的意思是希望在窗口标题更改时收到通知,以便您可以再次调用GetWindowText
  • 写恶意软件的请求是题外话。
  • 无论此代码的目的是什么,都不清楚您的问题是什么。你的问题是“如何检测前台窗口发生变化”

标签: c# title


【解决方案1】:

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

【讨论】:

  • 我不确定哪个更令人不安:您认为这是一个合理的解决方案,还是 OP 接受它作为一个合理的解决方案。我当然希望我永远不会遇到你们中的任何一个编写的软件。
  • 那么您愿意详细说明一个合适的解决方案是什么样的吗?
  • 它在我发布的副本中。
猜你喜欢
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多