8 out of 16 rated this helpful - Rate this topic

GetForegroundWindow function

Applies to: desktop apps only

Retrieves a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.

Syntax

HWND WINAPI GetForegroundWindow(void);

Parameters

This function has no parameters.

Return value

Type:

Type: HWND

The return value is a handle to the foreground window. The foreground window can be NULL in certain circumstances, such as when a window is losing activation.

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
SetForegroundWindow
Conceptual
Windows

 

 

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
vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetForegroundWindow() As IntPtr End Function
C# syntax
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr GetForegroundWindow();
What happens when there are two windows stacked side by side or on mult-monitor?
Can there be two forground windows? Is there anyway to identiy windows which are full visible and not hidden behind other windows?
Yet another case
when GetForegroundWindow returns null is when you connect to a server with remote desktop and minimize your remote desktop window. It's very annoying.
Another Case When GetForegroundWindow Returns NULL
I've been told that GetForegroundWindow will return NULL if the screen is locked.