This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DllImport("user32.dll")] | |
public static extern IntPtr GetForegroundWindow(); | |
[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)] | |
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); | |
[DllImport("user32.dll")] | |
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); | |
int processid; | |
try{ | |
GetWindowThreadProcessId(GetForegroundWindow(), out processid); | |
if (0 != processid){ | |
Process p = Process.GetProcessById(processid); | |
System.Diagnostics.Debug.Write(processid + ":"); | |
System.Diagnostics.Debug.WriteLine(p.MainModule.FileVersionInfo.ProductName);//アプリケーションの名前が出てきます | |
} | |
else{ | |
System.Diagnostics.Debug.Write(processid + ":Sleep"); | |
} | |
} | |
catch (System.ComponentModel.Win32Exception e){ | |
System.Diagnostics.Debug.Writeln("何もしない"); | |
} |