Foreground Vs Active window

JavaMan picture JavaMan · Oct 15, 2010 · Viewed 23k times · Source

In Windows, what is the difference between foreground and active window? To be specific, under what circumstances can a foreground window not be an active window? If the 2 terms are referring to the same concept why there're 2 terms.

The msdn documentation here mentions "clicking a window, or by using the ALT+TAB or ALT+ESC key combination" makes a window active as well as foreground. There is nothing explicitly about the difference between the 2 terms.Check MSDN.

Answer

jamesdlin picture jamesdlin · Feb 21, 2015

The active window (the result of GetActiveWindow()) is the window attached to the calling thread that gets input. The foreground window (the result of of GetForegroundWindow()) is the window that's currently getting input regardless of its relationship to the calling thread. The active window is essentially localized to your application; the foreground window is global to the system.

For example, if a window belonging to another process is the foreground, calling GetActiveWindow() from within your own process will return NULL.

I believe that it's true that being the foreground window implies being the active window, but the converse is not true. Also note that in modern Windows, applications generally cannot use SetForegroundWindow() to steal focus from another process (unless that process has explicitly given permission via AllowSetForegroundWindow).