取得窗口的句柄.类.名称等

建立三个label1/label2/lebel3/
名称分别为窗口句柄/类/标题/
建立一个text窗口
建立二个command按钮,一为开始抓取。一为退出
声明:
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 Sub Command1_Click()
If Command1.Caption = "开始抓取(&S)" Then
Timer1.Enabled = True
Command1.Caption = "停止抓取(&S)"
Else
Timer1.Enabled = False
Command1.Caption = "开始抓取(&S)"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, &H1 or &H2 '使窗体位于最顶端
End Sub
Private Sub Timer1_Timer()
On Error Resume Next
Dim tPoint As POINTAPI
Dim hWin As Long
Dim str As String * 255
Dim Abc As String * 64000
Dim Txt(64000) As Byte
GetCursorPos tPoint '获得当前鼠标位置
hWin = WindowFromPoint(tPoint.x, tPoint.y) '获得窗口名柄
If hWin = Me.hwnd or hWin = Command1.hwnd or hWin = Command2.hwnd or hWin = Text1.hwnd Then Exit Sub '确定窗口不在 Form1 中
GetClassName hWin, str, 255 '获得窗口类
SendMessage hWin, &HD, 64000, Txt(0) '获得窗口标题(也可使用 API 函数:GetWindowText,但效果不佳)
Label1.Caption = "窗口名柄: " & hWin
Label2.Caption = "窗口类: " & str
Text1.Text = StrConv(Txt, vbUnicode)
End Sub

未经允许不得转载:Sleep's Blog » 取得窗口的句柄.类.名称等

相关文章

评论 (0)

2 + 8 =

*