'方法1 调用 msgbox GetWindowTitle(句柄)
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Function GetWindowTitle(ByVal hwnd As Long) As String
Dim title As String
Dim length As Long
' 获取标题长度
length = GetWindowTextLength(hwnd) + 1
' 分配内存
title = Space(length)
' 获取窗口标题
length = GetWindowText(hwnd, title, length)
' 返回标题
GetWindowTitle = Left$(title, length)
End Function
'方法2 调用 msgbox GetTextBoxText(句柄)
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 Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Function GetTextBoxText(ByVal hWnd As Long) As String
Dim textLength As Long
Dim buffer As String
' 获取文本框内容的长度
textLength = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, ByVal 0) + 1
' 分配足够的缓冲区来存储文本框内容
buffer = Space(textLength)
' 获取文本框内容
SendMessage hWnd, WM_GETTEXT, textLength, ByVal buffer
' 返回文本框内容
GetTextBoxText = Left$(buffer, textLength - 1)
End Function