2016-05-04 15 views
-1

私はWindowsフォームを使用していて、ユーザーが他のアプリケーションの名前を取得できるかどうか、またはフォーカスしているかどうかは疑問でした。 たとえば、アプリケーションを実行すると、最近開始され、現在フォーカスが当てられているアプリケーションの名前を取得する必要があります。 Googleで検索しましたが、適切な解決策が見つかりませんでした。現在フォーカスのあるアプリケーション名を取得するにはどうすればいいですか?

ありがとうございます!

答えて

5
[DllImport("user32.dll")] 
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

public string GetActiveWindowTitle() 
{ 
    var handle = GetForegroundWindow(); 
    string fileName = ""; 
    string name = ""; 
    uint pid = 0; 
    GetWindowThreadProcessId(handle, out pid); 

    Process p = Process.GetProcessById((int)pid); 
    var processname = p.ProcessName; 

    switch (processname) 
    { 
     case "explorer": //metro processes 
     case "WWAHost": 
      name = GetTitle(handle); 
      return name; 
     default: 
      break; 
    } 
    string wmiQuery = string.Format("SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId LIKE '{0}'", pid.ToString()); 
    var pro = new ManagementObjectSearcher(wmiQuery).Get().Cast<ManagementObject>().FirstOrDefault(); 
    fileName = (string)pro["ExecutablePath"]; 
    // Get the file version 
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(fileName); 
    // Get the file description 
    name = myFileVersionInfo.FileDescription; 
    if (name == "") 
     name = GetTitle(handle); 

return name; 
} 

public string GetTitle(IntPtr handle) 
{ 
string windowText = ""; 
    const int nChars = 256; 
    StringBuilder Buff = new StringBuilder(nChars); 
    if (GetWindowText(handle, Buff, nChars) > 0) 
    { 
     windowText = Buff.ToString(); 
    } 
    return windowText; 
} 
+2

ありがとうございます! 'GetForegroundWindow();'メソッドと 'ManagementObjectSearcher'クラスについて説明してください。 – CodeIt

関連する問題