'以下代码可以列出所有IE的窗口标题Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As LongSub main() EnumWindows AddressOf EnumProc, 0 End SubPrivate Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim sWindowClass As String Dim sWindowText As String Dim nCount As Long sWindowClass = Space(81) sWindowText = Space(257) nCount = GetClassName(hwnd, sWindowClass, 80) sWindowClass = Mid(sWindowClass, 1, nCount) If sWindowClass = "IEFrame" Then nCount = GetWindowText(hwnd, sWindowText, 256) sWindowText = Mid(sWindowText, 1, nCount) msgbox sWindowText end if EnumProc = TrueEnd Function
用FindWindowEx查找 IE的类名为“IEFrame”
用findwindowex,getwindowtextlength,getwindowtext
'引用 Microsoft Internet Controls Option Explicit Private Sub Command1_Click() Dim xx As New SHDocVw.ShellWindows Dim i As Long For i = 0 To xx.Count - 1 If VBA.TypeName(xx.Item(i).Document) = "HTMLDocument" Then Debug.Print xx.Item(i).Document.Title End If Next i End Sub
把下面代码贴去用,加个time和textbox控件就行了,随便什么窗口都行Option Explicit Private Declare Function GetWindowText& Lib "user32" Alias "GetWindowTextA" _ (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) Private Declare Function GetForegroundWindow& Lib "user32" ()Private Sub Timer1_Timer() Dim dl As Long Dim MyHwnd As Long Dim MyCaption As String MyCaption = Space(256) MyHwnd& = GetForegroundWindow() dl& = GetWindowText(MyHwnd, MyCaption, 255) Label2.Caption = MyCaption Label2.ToolTipText = Label2.Caption End SubPrivate Sub Form_Load() Text1.Text = "未定义点击区域" Text1.Enabled = False End Sub
对api不太感兴趣,我喜欢数据库
EnumWindows AddressOf EnumProc, 0
End SubPrivate Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sWindowClass As String
Dim sWindowText As String
Dim nCount As Long sWindowClass = Space(81)
sWindowText = Space(257)
nCount = GetClassName(hwnd, sWindowClass, 80)
sWindowClass = Mid(sWindowClass, 1, nCount) If sWindowClass = "IEFrame" Then
nCount = GetWindowText(hwnd, sWindowText, 256)
sWindowText = Mid(sWindowText, 1, nCount)
msgbox sWindowText
end if
EnumProc = TrueEnd Function
IE的类名为“IEFrame”
Option Explicit
Private Sub Command1_Click()
Dim xx As New SHDocVw.ShellWindows
Dim i As Long
For i = 0 To xx.Count - 1
If VBA.TypeName(xx.Item(i).Document) = "HTMLDocument" Then
Debug.Print xx.Item(i).Document.Title
End If
Next i
End Sub
你的New SHDocVw.ShellWindows是什么东西啊,我这里不能运行啊。请具体指教好吗?
Private Declare Function GetWindowText& Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long)
Private Declare Function GetForegroundWindow& Lib "user32" ()Private Sub Timer1_Timer()
Dim dl As Long
Dim MyHwnd As Long
Dim MyCaption As String
MyCaption = Space(256)
MyHwnd& = GetForegroundWindow()
dl& = GetWindowText(MyHwnd, MyCaption, 255)
Label2.Caption = MyCaption
Label2.ToolTipText = Label2.Caption
End SubPrivate Sub Form_Load()
Text1.Text = "未定义点击区域"
Text1.Enabled = False
End Sub