《VB》API如何获取窗口内控件的句柄?

2024-11-29 22:41:00
推荐回答(1个)
回答1:

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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 Type POINTAPI
x As Long
y As Long
End Type
Dim n As POINTAPI
Dim a As Long
Private Sub Form_Load()
Timer1.Interval = 100
Label1.Caption = "移动鼠标指针"
End Sub

Private Sub Timer1_Timer()
GetCursorPos n
a = WindowFromPoint(n.x, n.y)
If a <> 0 Then
Dim s As String
s = String(100, Chr(0))
GetWindowText a, s, 100
Label1.Caption = "目标标题或文本: " & Trim(s)
Label2.Caption = "目标句柄为 " & a
End If
End Sub