CodeItBetter Programming Another VB Programming Blog

How to get Caption and Thread ID of Window under cursor

Posted on August 11, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'System & API - How to get Caption and Thread ID of Window under cursor

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
 
'Add a Text Box, a Command Button, two Labels, and a 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.

Private Sub Command1_Click()
    Dim sAppName As String
    Dim lState As Long, 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, lProcessId As Long, lThreadId As Long, 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
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.