枚举窗体即可新建一个模块 Option Explicit Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean 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 Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean Dim strTmp As String strTmp = GetWindowCaption(hwnd) If strTmp <> "" Then frmMain.listWindows.AddItem strTmp EnumWindowsProc = True End FunctionPrivate Function GetWindowCaption(ByVal hwnd As Long) As String Dim strText As String, ret As Long ret = GetWindowTextLength(hwnd) If ret > 0 Then strText = Space(ret) GetWindowText hwnd, strText, ret + 1 strText = Left(strText, ret) GetWindowCaption = strText Else GetWindowCaption = "" End If End Function新建一窗体命名frmMain在上面拖一个List1命名为:listWindows
'新建一模块代码如下: Option Explicit Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean Dim S As String S = String(80, 0) Call GetWindowText(hwnd, S, 80) S = Left(S, InStr(S, Chr(0)) - 1) If Len(S) > 0 Then Form1.Text1.Text = Form1.Text1.Text & S & "--->" & hwnd & vbNewLine EnumWindowsProc = True End Function '新建一窗体,加一按钮和一文本框。窗体代码如下: Option ExplicitPrivate Sub Command1_Click() Text1.Text = "" EnumWindows AddressOf EnumWindowsProc, 0&End Sub
chenhui530 陈辉 的代码须在窗体上加个按钮再附上代码 Private Sub Command1_Click() Call EnumWindows(AddressOf EnumWindowsProc, 0) End Sub不过还需加层过滤。
'代码全给你吧。其实也就比他们多了个过滤功能而已。'模块代码 Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Const SW_SHOW = 5 Public Const SW_RESTORE = 9 Public Const GW_OWNER = 4 Public Const GWL_HWNDPARENT = (-8) Public Const GWL_EXSTYLE = (-20) Public Const WS_EX_TOOLWINDOW = &H80 Public Const WS_EX_APPWINDOW = &H40000 Public Const LB_ADDSTRING = &H180 Public Const LB_SETITEMDATA = &H19A Public Const EWX_FORCE = 4 Public Const EWX_LOGOFF = 0 Public Const EWX_REBOOT = 2 Public Const EWX_SHUTDOWN = 1 Const MAX_PATH& = 260 Private Const SW_HIDE = 0 Private Const WM_CLOSE = &H10 Public HHWnd As LongPublic Function WhichWindowsCallBack(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim lReturn As Long Dim lExStyle As Long Dim bNoOwner As Boolean Dim sWindowText As String If hwnd <> Form1.hwnd Then If IsWindowVisible(hwnd) Then If GetParent(hwnd) = 0 Then bNoOwner = (GetWindow(hwnd, GW_OWNER) = 0) lExStyle = GetWindowLong(hwnd, GWL_EXSTYLE) If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or _ ((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then sWindowText = Space$(256) lReturn = GetWindowText(hwnd, sWindowText, Len(sWindowText)) If lReturn Then sWindowText = Left$(sWindowText, lReturn) lReturn = SendMessage(lParam, LB_ADDSTRING, 0, ByVal sWindowText) Call SendMessage(lParam, LB_SETITEMDATA, lReturn, ByVal hwnd) End If End If End If End If End If HHWnd = hwnd WhichWindowsCallBack = True End FunctionPublic Function fEnumWindows(lst As ListBox) As Long With lst .Clear Call EnumWindows(AddressOf WhichWindowsCallBack, .hwnd) fEnumWindows = .ListCount End With End Function '窗体中代码,直接托个ListBox在窗体里运行就看到效果了。 Private Sub Form_Load() Call fEnumWindows(List1) End Sub
Option Explicit
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
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
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim strTmp As String
strTmp = GetWindowCaption(hwnd)
If strTmp <> "" Then frmMain.listWindows.AddItem strTmp
EnumWindowsProc = True
End FunctionPrivate Function GetWindowCaption(ByVal hwnd As Long) As String
Dim strText As String, ret As Long
ret = GetWindowTextLength(hwnd)
If ret > 0 Then
strText = Space(ret)
GetWindowText hwnd, strText, ret + 1
strText = Left(strText, ret)
GetWindowCaption = strText
Else
GetWindowCaption = ""
End If
End Function新建一窗体命名frmMain在上面拖一个List1命名为:listWindows
Option Explicit
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 LongPublic Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String
S = String(80, 0)
Call GetWindowText(hwnd, S, 80)
S = Left(S, InStr(S, Chr(0)) - 1)
If Len(S) > 0 Then Form1.Text1.Text = Form1.Text1.Text & S & "--->" & hwnd & vbNewLine
EnumWindowsProc = True
End Function
'新建一窗体,加一按钮和一文本框。窗体代码如下:
Option ExplicitPrivate Sub Command1_Click()
Text1.Text = ""
EnumWindows AddressOf EnumWindowsProc, 0&End Sub
陈辉
的代码须在窗体上加个按钮再附上代码
Private Sub Command1_Click()
Call EnumWindows(AddressOf EnumWindowsProc, 0)
End Sub不过还需加层过滤。
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Const SW_SHOW = 5
Public Const SW_RESTORE = 9
Public Const GW_OWNER = 4
Public Const GWL_HWNDPARENT = (-8)
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_TOOLWINDOW = &H80
Public Const WS_EX_APPWINDOW = &H40000
Public Const LB_ADDSTRING = &H180
Public Const LB_SETITEMDATA = &H19A
Public Const EWX_FORCE = 4
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1
Const MAX_PATH& = 260
Private Const SW_HIDE = 0
Private Const WM_CLOSE = &H10
Public HHWnd As LongPublic Function WhichWindowsCallBack(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim lReturn As Long
Dim lExStyle As Long
Dim bNoOwner As Boolean
Dim sWindowText As String If hwnd <> Form1.hwnd Then
If IsWindowVisible(hwnd) Then
If GetParent(hwnd) = 0 Then
bNoOwner = (GetWindow(hwnd, GW_OWNER) = 0)
lExStyle = GetWindowLong(hwnd, GWL_EXSTYLE) If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or _
((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then sWindowText = Space$(256)
lReturn = GetWindowText(hwnd, sWindowText, Len(sWindowText))
If lReturn Then sWindowText = Left$(sWindowText, lReturn)
lReturn = SendMessage(lParam, LB_ADDSTRING, 0, ByVal sWindowText)
Call SendMessage(lParam, LB_SETITEMDATA, lReturn, ByVal hwnd)
End If
End If
End If
End If
End If
HHWnd = hwnd
WhichWindowsCallBack = True
End FunctionPublic Function fEnumWindows(lst As ListBox) As Long
With lst
.Clear
Call EnumWindows(AddressOf WhichWindowsCallBack, .hwnd)
fEnumWindows = .ListCount
End With
End Function
'窗体中代码,直接托个ListBox在窗体里运行就看到效果了。
Private Sub Form_Load()
Call fEnumWindows(List1)
End Sub