/// <summary> /// This variant of IsWindowActivated simply checks whether a specified window handle /// is currently focused rather than whether any window belonging to the current process /// is focused. /// </summary> /// <returns>Returns true if the specified handle has focus, else false.</returns> public static bool IsWindowActivated(IntPtr windowHandle) { // Obtain the active window handle. IntPtr activatedHandle = WindowFunctions.GetForegroundWindow(); // Check if any window is active. if (activatedHandle == IntPtr.Zero) { return(false); } // Compare the process identifiers of active window and our process. return(activatedHandle == windowHandle); }
/// <summary> /// Checks whether the current application is activated. /// The method compares the current active foreground window to the /// window thread of the current caller. /// /// The function is not specific /// to any technology and works for both child windows and the current window, /// also independently of the thread which owns a specific window. /// </summary> /// <returns>Returns true if the current application has focus/is foreground/is activated. Else false.</returns> public static bool IsWindowActivated() { // Obtain the active window handle. IntPtr activatedHandle = WindowFunctions.GetForegroundWindow(); // Check if any window is active. if (activatedHandle == IntPtr.Zero) { return(false); } // Retrieve unique identifier for this process. int currentProcessIdentifier = System.Diagnostics.Process.GetCurrentProcess().Id; // Retrieve the process identifier for the active window. WindowFunctions.GetWindowThreadProcessId(activatedHandle, out int activeProcessIdentifier); // Compare the process identifiers of active window and our process. return(activeProcessIdentifier == currentProcessIdentifier); }