Win32 API question answered: What is the difference between GetActiveWindow and GetForegroundWindow?
Posted: (EET/GMT+2)
I recently needed to have a piece of code that figures out the currently active window with which the user is currently working with. As far as I'm aware, there isn't anything in .NET for this (except maybe for WPF, where you can use this trick for the application itself: "Application.Current.Windows.OfType
Win32 API contains two useful functions for this: GetActiveWindow and GetForegroundWindow.
With a quick glimpse, both functions appear to do the same thing. However, there's a difference that affects how these functions behave when the currently active window the user is working with isn't the same application making the API function call.
More specifically, the GetActiveWindow function only looks at the current thread input queue, and the GetForegroundWindow function looks at the "global" situation. In regular Win32 API applications, there's often only a single thread, but this also oftentimes applies to .NET and C# applications as well.
For more details, refer to the always useful MSDN blog post by Raymond Chen.