WindowFromPoint Function

Declare Function WindowFromPoint Lib "user32.dll" (ByVal xPoint As Long, ByVal yPoint As Long) As Long

Platforms: Win 32s, Win 95/98, Win NT

WindowFromPoint determines the handle of the window located at a specific point on the screen. Note that the active window could be a text box, list box, button, or some other object sitting inside a program window. In this case, the handle returned will be to this control and not the program window. If successful, the function returns the handle to the window at that point. If there is no window at that point, or if an error occured, the function instead returns 0.

xPoint
The x-coordinate of the point to look for a window at.
yPoint
The y-coordinate of the point to look for a window at.

Example:

' Display the title bar text of whatever window the mouse
' cursor is currently over.  Note that this could be a control on a program window.
Dim mousepos As POINT_TYPE  ' coordinates of the mouse cursor
Dim wintext As String, slength As Long  ' receive title bar text and its length
Dim hwnd As Long  ' handle to the window found at the point
Dim retval As Long  ' return value

' Determine the window the mouse cursor is over.
retval = GetCursorPos(mousepos)  ' get the location of the mouse
hwnd = WindowFromPoint(mousepos.x, mousepos.y)  ' determine the window that's there
If hwnd = 0 Then  ' error or no window at that point
  Debug.Print "No window exists at that location."
  End
End If

' Display that window's title bar text
slength = GetWindowTextLength(hwnd)  ' get length of title bar text
wintext = Space(slength + 1)  ' make room in the buffer to receive the string
slength = GetWindowText(hwnd, wintext, slength + 1)  ' get the text
wintext = Left(wintext, slength)  ' extract the returned string from the buffer
Debug.Print "Title bar text of the window: "; wintext

Category: Windows

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/w/windowfrompoint.html