窗口按钮代码:
'按住按钮并拖动至窗口标题再松开,本窗口上显示松开时目标窗体的句柄与名称
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim NowPOINT As POINTAPI '记录鼠标松开时所在位置
Dim TheHwnd As Long '目标窗体句柄
Dim TheName As String '目标窗体名称
GetCursorPos NowPOINT '获取鼠标位置
TheHwnd = WindowFromPoint(NowPOINT.X, NowPOINT.Y) '获取鼠标位置所指窗口句柄
Cls
Print "目标句柄:" & TheHwnd
TheName = String(255, Chr(0)) '必要的
GetWindowText TheHwnd, TheName, 255 '获取目标窗体名称
Print "目标句柄:" & TheName
End Sub
模块代码:
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function WindowFromPoint Lib "user32" (ByVal XPoint As Long, ByVal yPoint As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Type POINTAPI
X As Long
Y As Long
End Type