- Option Explicit
- Private Declare Function GetDesktopWindow Lib "user32" () As Long
- Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
- Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
- 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 IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
- Private Const GW_HWNDNEXT = 2
- Private Const GW_CHILD = 5
- Private Sub Form_Load()
- '使用前在窗体上创建一个ListBox控件,名称为List1
- Dim hwnd As Long
- hwnd = GetDesktopWindow() '返回桌面的句柄
- hwnd = GetWindow(hwnd, GW_CHILD) '返回桌面的第一个子窗口
- Dim sWindowText As String
- Dim r As Long
- Do While hwnd <> 0
- If GetParent(hwnd) = 0 Then '顶层窗口
- If IsWindowVisible(hwnd) Then '窗口状态是显示的
- sWindowText = String(255, Chr(0))
- r = GetWindowText(hwnd, sWindowText, 255) '返回窗口的标题
- sWindowText = Left(sWindowText, r)
- If sWindowText <> "" Then List1.AddItem sWindowText
- End If
- End If
- hwnd = GetWindow(hwnd, GW_HWNDNEXT) '返回下一个窗口
- Loop
- End Sub
|