2 out of 3 rated this helpful - Rate this topic

WindowFromPoint function

Applies to: desktop apps only

Retrieves a handle to the window that contains the specified point.

Syntax

HWND WINAPI WindowFromPoint(
  __in  POINT Point
);

Parameters

Point [in]

Type: POINT

The point to be checked.

Return value

Type:

Type: HWND

The return value is a handle to the window that contains the point. If no window exists at the given point, the return value is NULL. If the point is over a static text control, the return value is a handle to the window under the static text control.

Remarks

The WindowFromPoint function does not retrieve a handle to a hidden or disabled window, even if the point is within the window. An application should use the ChildWindowFromPoint function for a nonrestrictive search.

Examples

For an example, see "Interface from Running Object Table" in About Text Object Model.

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
ChildWindowFromPoint
Conceptual
Windows
Other Resources
POINT
WindowFromDC

 

 

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
They all don't work correctly
It does not matter if you use WindowFromPoint, ChildWindowFromPoint, ChildWindowFromPointEx or RealChildWindowFromPoint. None of these functions returns ALWAYS the windows under the given point. These functions work in some cases, but none of them works correctly ALWAYS. The one the works best to obtain the window at the given point is RealChildWindowFromPoint. But also this function fails if you have a Dialog in a TabControl like in Microsoft`s Html Help Workshop for example. In this case the funtion returns the Tab Control although the given point lies in the smaller Dialog (ListBox). So if you need a realiable function that works corretcly ALWAYS you will end up writing your own code. You have to test with a lot of applications to find out if your code realy works correctly in all cases. Don't forget to test also RTL scenarios! It is a shame that Microsoft is not able to give a function that works correctly.
Comparison of different *WindowFromPoint* methods
Raymond Chen discusses the different methods here:

http://blogs.msdn.com/b/oldnewthing/archive/2010/12/30/10110077.aspx

"WindowFromPoint, ChildWindowFromPoint, RealChildWindowFromPoint, when will it all end?"

C# syntax
[DllImport("user32.dll", EntryPoint="WindowFromPoint", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr WindowFromPoint(POINTSTRUCT pt);
VB.net syntax
<DllImport("user32.dll", EntryPoint:="WindowFromPoint", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function WindowFromPoint(ByVal pt As POINTSTRUCT) As IntPtr End Function
C# declaration and example

The code below shows an example of how to use WindowFromPoint mixed with GetCursorPos to get the window your mouse is hovered over.

[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point lpPoint);

[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point lpPoint);

public static IntPtr GetWindowUnderCursor()
{
Point ptCursor = new Point();

if (!(PInvoke.GetCursorPos(out ptCursor)))
return IntPtr.Zero;

return WindowFromPoint(ptCursor);
}