Visual Basic Programming Code Examples
Visual Basic > Windows and Controls Code Examples
Returning the windows handle of a dialog at a specified point
Returning the windows handle of a dialog at a specified point
The following code demonstrates how to return the windows handle of the form/dialog at a specific location on the screen.
Option Explicit
Private Declare Function WindowFromPointA Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
'Purpose : Returns the windows handle of the window which is at the specified location
'Inputs : X The X coordinate of the window
' Y The Y coordinate of the window
'Outputs : Returns the handle of the window at the specified location, else returns 0.
Function WindowFromPoint(X As Long, Y As Long) As Long
WindowFromPoint = WindowFromPointA(X, Y)
End Function
'Purpose : Returns the window caption of a specified window.
'Inputs : lHwnd The handle of the window to determine the caption of.
'Outputs : Returns the window caption of a specified window.
Function DialogGetCaption(lHwnd As Long) As String
Const clMaxLen As Long = 255
Dim lLen As Long, sValue As String * clMaxLen
lLen = GetWindowText(lHwnd, sValue, clMaxLen)
If lLen Then
DialogGetCaption = Left$(sValue, lLen)
End If
End Function
'Demonstration routine
Sub Test()
Dim lHwnd As Long
lHwnd = WindowFromPoint(100, 100)
If lHwnd Then
Debug.Print "The window at point 100, 100 has the caption: "
Debug.Print DialogGetCaption(lHwnd)
End If
End Sub