A good solution is given by this answer to an identical question: fooobar.com/questions/159037 / ...
/// <summary>Returns true if the current application has focus, false otherwise</summary> public static bool ApplicationIsActivated() { var activatedHandle = GetForegroundWindow(); if (activatedHandle == IntPtr.Zero) { return false; // No window is currently activated } var procId = Process.GetCurrentProcess().Id; int activeProcId; GetWindowThreadProcessId(activatedHandle, out activeProcId); return activeProcId == procId; } [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
Currently, the decision made by Euric does not work if your program displays a dialog box or has removable windows (for example, if you use the window docking infrastructure).
source share