11 out of 24 rated this helpful - Rate this topic

SetForegroundWindow function

Applies to: desktop apps only

Brings the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.

Syntax

BOOL WINAPI SetForegroundWindow(
  __in  HWND hWnd
);

Parameters

hWnd [in]

Type: HWND

A handle to the window that should be activated and brought to the foreground.

Return value

Type:

Type: BOOL

If the window was brought to the foreground, the return value is nonzero.

If the window was not brought to the foreground, the return value is zero.

Remarks

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.

A process that can set the foreground window can enable another process to set the foreground window by calling the AllowSetForegroundWindow function. The process specified by dwProcessId loses the ability to set the foreground window the next time the user generates input, unless the input is directed at that process, or the next time a process calls AllowSetForegroundWindow, unless that process is specified.

The foreground process can disable calls to SetForegroundWindow by calling the LockSetForegroundWindow function.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
AllowSetForegroundWindow
GetForegroundWindow
LockSetForegroundWindow
SetActiveWindow
Conceptual
Windows
Other Resources
FlashWindowEx

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
C# syntax
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
In the other hand, if you have access to the Form, is better to use the proper c# function:
http://jorgearimany.blogspot.com/2010/10/win32-setforegroundwindow-equivalent-in.html#more


.NET
In .NET if you have access to the Form you wan to bring to fron you can do it easily calling the proper native function:

http://jorgearimany.blogspot.com/2010/10/win32-setforegroundwindow-equivalent-in.html#more
Force Sample

This API only works for the owner process. For forcing a window that belongs to another process see the function called

ForceForegroundWindow

here:
http://www.asyncop.com/MTnPDirEnum.aspx?treeviewPath=%5bo%5d+Open-Source%5cWinModules%5cInfrastructure%5cSystemAPI.cpp

a failed SetForegroundWindow can change the keyboard focus
I've noticed some weird behaviors with SetForegroundWindow under Windows 7.  Specifically, it seems that even if SetForegroundWindow() fails, it may still alter the current keyboard focus.  For example, when running this code outside the debugger, I trigger the "that's really weird!" exception:

if(hWnd!=GetFocus()) {  if(!SetForegroundWindow(hWnd)) {   if( hWnd==GetFocus() )  {  throw std::runtime_error("that's really weird!");    }  }   }

Technically, I wouldn't quite call this an API bug, as the documentation doesn't say anything, one way or another, about whether or not a failed SetForegroundWindow call should change the keyboard focus.  But, the behavior is certainly a bit strange.$0$0
SetForegroundWindow Function (Windows)
SetForegroundWindow Function (Windows)
    
vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
Public Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

Syntax using Declare

Public Declare Ansi Function SetForegroundWindow Lib "user32.dll" Alias "SetForegroundWindow" (ByVal hwnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean