2014-11-06 131 views
-4

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

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

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

+5

'我正在写一个键盘记录程序--- --->祝您在这里获得帮助 – 2014-11-06 16:22:42

+0

您的意思是您希望在窗口标题更改时得到通知,以便您可以再次调用'GetWindowText'? – 2014-11-06 16:24:02

+3

撰写恶意软件的请求不太明确。 – MatthewMartin 2014-11-06 16:25:28

回答

-2

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

+2

我不确定哪个更令人不安:您认为这是一个合理的解决方案,或者OP将其视为合理的解决方案。我当然希望我永远不会碰到任何一个人编写的软件。 – 2014-11-07 04:06:21

+0

那么你是否在意详细说明一个合适的解决方案会是什么样子? – jules0075 2014-11-07 15:50:12

+1

这是在我发布的重复。 – 2014-11-07 15:53:22