Foreground Vs Active window
Solution 1:
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
).
Solution 2:
I find the description in MSDN a bit confusing as well but here is my revised take:
First a foreground and background window have nothing to do with active windows, it has to do with threading, see below. So it is technically possible to have background window as an active window however it is confusing and the system doesn't do this for you, instead your app needs to call e.g. SetWindowPos to make the background window active.
The system can only have one active top-level window at a time, the system will activate the top-level window if you are working on a child window. All input is then directed to the active window and then normally passed to the child window.
/----------------------\
| |
| FOREGROUND WINDOW |--\
| | |
\----------------------/ |
| BACKGROUND WINDOW |
\-----------------------/
/----------------------\
| |
| ACTIVE WINDOW |--\
| | |
\----------------------/ |
| BACKGROUND WINDOW |
\-----------------------/
From MSDN
Active Window
An active window is the top-level window of the application with which the user is currently working. To allow the user to easily identify the active window, the system places it at the top of the z-order and changes the color of its title bar and border to the system-defined active window colors. Only a top-level window can be an active window. When the user is working with a child window, the system activates the top-level parent window associated with the child window.
Foreground/Background
Each process can have multiple threads of execution, and each thread can create windows. The thread that created the window with which the user is currently working is called the foreground thread, and the window is called the foreground window. All other threads are background threads, and the windows created by background threads are called background windows.