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

This function puts the thread that created the specified window into the foreground and activates the window.

Syntax

BOOL SetForegroundWindow(
  HWND 
hWnd 
);

Parameters

hWnd

[in] Handle to the window that should be activated and brought to the foreground. For more information, see the Remarks section.

Return Value

Nonzero indicates that the window was brought to the foreground. Zero indicates that the window was not brought to the foreground.

Remarks

The foreground window is the window at the top of the z-order. It is the window that the user is working with. In a preemptive multitasking environment, you should generally let the user control which window is the foreground window.

The thread that owns the window is not given a priority boost.

If the window being set to the foreground is minimized, the window may not be visible to the end user. To restore the window to a visible state, ShowWindowshould be called.

This function does not reactivate the last active owned window by default. To ensure that the last active owned window is reactivated, combine the window handle with 0x01 using the logical OR operator. For example:

Copy Code
SetForegroundWindow((HWND)(((ULONG) hwnd) | 0x01) );

Calling PeekMessagebefore the top level window of an application is created will result in the window being created at the back of the z-order. You will need to explicitly call SetForegroundWindowto show the window when it is created after a call to PeekMessage. If the application already has a window in the foreground, then the new window will be created in the foreground.

SetForegroundWindowshould not be called on child windows.

Requirements

Header winuser.h
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also