Get Caption And Thread ID Of Window Under Cursor

'Add 1 Text Box, 1 Command Button, 2 Labels, And 1 Timer Control to your Form.
'Set Timer Interval property to 500.
'Label1 will display the caption of the window under the cursor.
'Label2 will display the window's thread ID.
'Insert window's caption to Text1 and press the button to activate it.
'Insert the following code to your form:

Option Explicit

Const WM_COMMAND = &H111
Const MAX_PATH = 260
Const SW_SHOW = 5
Const SW_RESTORE = 9
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const SW_SHOWNORMAL = 1

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type

Private lpwndpl As WINDOWPLACEMENT
Private CursorLoc As POINTAPI

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (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
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd _
As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As _
Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As _
Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal _
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As _
Long) As Long

Private Sub Command1_Click()
Dim sAppName As String
Dim lState As Long
Dim lHandle As Long
sAppName = Trim$(Text1)
If sAppName = "" Then Exit Sub
'Find the window with the current caption.
lpwndpl.Length = 44
lHandle = FindWindow(vbNullString, sAppName)
If lHandle = 0 Then Exit Sub
'Get the window's state and activate it.
lState = GetWindowPlacement(lHandle, lpwndpl)
Select Case lpwndpl.showCmd
Case SW_SHOWMINIMIZED
Call ShowWindow(lHandle, SW_RESTORE)
Case SW_SHOWNORMAL, SW_SHOWMAXIMIZED
Call ShowWindow(lHandle, SW_SHOW)
End Select
Call SetForegroundWindow(lHandle)
End Sub

Private Sub Timer1_Timer()
Dim lHandle As Long
Dim lProcessId As Long
Dim lThreadId As Long
Dim lStrLen As Long
Dim sText As String
' Get the cursor's coordinates.
' Get the handle of the window at those coordinates.

lStrLen = MAX_PATH
Call GetCursorPos(CursorLoc)
lHandle = WindowFromPoint(CursorLoc.x, CursorLoc.y)
' Get the window's caption.
sText = Space$(MAX_PATH)
Call GetWindowText(lHandle, sText, lStrLen)
Label1 = Left$(sText, lStrLen)
' Get the window's process and thread ID.
lThreadId = GetWindowThreadProcessId(lHandle, lProcessId)
Label2 = CStr(lProcessId)
End Sub

Go Back