2014-11-06 128 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