=======================窗体==================== Option ExplicitPrivate Sub Command1_Click() EnumWindows AddressOf EnumProc, 0 '枚举窗口列表中的所有父窗口 End Sub=======================模块======================Option ExplicitDeclare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) As Long Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongDim buf As String Dim title As StringPublic Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean '遍查主窗口 Dim buf As String * 1024 Dim length As Long length = GetWindowText(app_hwnd, buf, Len(buf)) title = Left$(buf, length) If Left(title, 2) = "QQ" Then '判断是否为 OICQ 窗口 MsgBox "QQ" End If EnumProc = 1 End Function
:) 提供一种方法: Option Explicit Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Dim CptWnd() As String'获取含指定字符指定窗口句柄 Public Sub mGetWindow(ByVal cName As String, ByRef fText As String) Dim myStr As String * 255 Dim strLen As String Dim bWnd As Long Dim bWndback As Long Dim i As Long ReDim CptWnd(1, 0) strLen = Len(myStr) bWndback = 0: i = 0 Do '获取子窗口标志 bWnd = FindWindowEx(0, bWndback, cName, vbNullString) If bWnd <> 0 Then '获取子窗口标题 GetWindowText bWnd, myStr, strLen myStr = Trim(myStr) If InStr(myStr, fText) Then ReDim Preserve CptWnd(1, i) CptWnd(0, i) = myStr CptWnd(1, i) = bWnd Me.Print bWnd i = i + 1 'Exit Do End If Else Exit Do End If bWndback = bWnd Loop End SubPrivate Sub Form_Load() Dim i As Long mGetWindow vbNullString, "Visual" For i = 0 To UBound(CptWnd, 2) ' - 1 Debug.Print CptWnd(1, i) & " " & CptWnd(0, i) Next End Sub结束语,这种方法可以列出标题中喊有指定字符(这里是"Visual")的窗体(包括子窗,“控件”),也可以列出具有指定类名的。如果不想要子窗,可以把FINDWINDOWSEX换成FINDWINDOW。
Option ExplicitPrivate Sub Command1_Click()
EnumWindows AddressOf EnumProc, 0 '枚举窗口列表中的所有父窗口
End Sub=======================模块======================Option ExplicitDeclare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongDim buf As String
Dim title As StringPublic Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean '遍查主窗口
Dim buf As String * 1024
Dim length As Long length = GetWindowText(app_hwnd, buf, Len(buf))
title = Left$(buf, length) If Left(title, 2) = "QQ" Then '判断是否为 OICQ 窗口
MsgBox "QQ"
End If
EnumProc = 1
End Function
提供一种方法:
Option Explicit
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Dim CptWnd() As String'获取含指定字符指定窗口句柄
Public Sub mGetWindow(ByVal cName As String, ByRef fText As String)
Dim myStr As String * 255
Dim strLen As String
Dim bWnd As Long
Dim bWndback As Long
Dim i As Long
ReDim CptWnd(1, 0)
strLen = Len(myStr)
bWndback = 0: i = 0
Do
'获取子窗口标志
bWnd = FindWindowEx(0, bWndback, cName, vbNullString)
If bWnd <> 0 Then
'获取子窗口标题
GetWindowText bWnd, myStr, strLen
myStr = Trim(myStr)
If InStr(myStr, fText) Then
ReDim Preserve CptWnd(1, i)
CptWnd(0, i) = myStr
CptWnd(1, i) = bWnd
Me.Print bWnd
i = i + 1
'Exit Do
End If
Else
Exit Do
End If
bWndback = bWnd
Loop
End SubPrivate Sub Form_Load()
Dim i As Long
mGetWindow vbNullString, "Visual"
For i = 0 To UBound(CptWnd, 2) ' - 1
Debug.Print CptWnd(1, i) & " " & CptWnd(0, i)
Next
End Sub结束语,这种方法可以列出标题中喊有指定字符(这里是"Visual")的窗体(包括子窗,“控件”),也可以列出具有指定类名的。如果不想要子窗,可以把FINDWINDOWSEX换成FINDWINDOW。