以记事本为例,系统中有多个记事本在运行。
模块代码:
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 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 Const GW_OWNER = 4
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public WndCaption As String
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String
Dim a As Long
Dim v As Long
S = String(255, 0)
Call GetWindowText(hwnd, S, 255)
S = Left(S, InStr(S, Chr(0)) - 1)
v = GetWindow(hwnd, GW_OWNER)
a = IsWindowVisible(hwnd)
If Len(S) > 0 And a <> 0 And v = 0 And Right(S, 3) = "记事本" Then
WndCaption = WndCaption & S & vbCrLf
End If
EnumWindowsProc = True
End Function
窗体代码:
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0&
MsgBox WndCaption
End Sub