Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

There are a number of ways that a thread can become the foreground thread. If an application calls the SetForegroundWindowfunction and specifies a top-level window, the thread that owns the window becomes the foreground thread and the window becomes its active window. This function also moves the window to the top of the z-order. You can use SetForegroundWindowon any top-level window.

In most cases, if the user chooses a window, the system will place that window in the foreground. The thread that created the window becomes the foreground thread. If the foreground window is hidden or destroyed, the system designates another window as the foreground window. In that case, the new foreground window thread becomes the foreground thread. Call the GetForegroundWindowfunction to retrieve the current foreground window.

In general, an application thread does not need to set the foreground window explicitly. The system sets the foreground window when the user selects and closes windows. Call the SetActiveWindowfunction to activate a window. If the calling thread is the foreground thread, the new active window automatically becomes the foreground window. When the activation changes, the system sends a WM_ACTIVATEmessage to both the deactivated and activated windows. A thread can call the GetActiveWindowfunction to access its active window.

An application thread calls the SetFocusfunction to move the focus between the windows that the thread created. When the focus changes, the system sends a WM_KILLFOCUSmessage to the window that loses the focus. It sends a WM_SETFOCUSmessage to the window that gains the focus.

The system ensures that the focus window is always the active window or a descendant of the active window. If the focus changes to a window with a different top-level ancestor, the system first changes the activation, and then it changes the focus.

See Also