Hi,
Thank you for your update.
How the Task Manager get the window list is undocumented. A reasonable
heuristic is windows which are visible, which do not have an owner, and
which do have a caption. My previous code is missing the condition "do not
have an owner". To get the owner of a window, we can use "GetWindow()" with
GW_OWNER as parameter.
Then we have to deal with the window "Program Manager": it's still present
for reasons of compatibility. We can ignore it by checking its window class
name: "Progman".
So the complete code listing would be:
Delegate Function EnumWindowsProc(ByVal hWnd As System.IntPtr, ByVal
lParam As IntPtr) As Boolean
Declare Auto Function EnumWindows Lib "user32" (ByVal lpEnumFunc As
EnumWindowsProc, ByVal lParam As IntPtr) As Boolean
Declare Auto Function IsWindowVisible Lib "user32" (ByVal hWnd As
IntPtr) As Boolean
Declare Auto Function GetWindowText Lib "user32" (ByVal hwnd As IntPtr,
ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
Declare Auto Function GetWindowTextLength Lib "user32" (ByVal hwnd As
IntPtr) As Integer
Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hwnd As
IntPtr, ByVal nIndex As Int32) As Int32
Declare Auto Function GetParent Lib "user32" (ByVal hwnd As IntPtr) As
IntPtr
Declare Auto Function GetWindow Lib "user32" (ByVal hwnd As IntPtr,
ByVal wCmd As Int32) As IntPtr
Const GW_OWNER As Int32 = 4
Declare Auto Function GetClassName Lib "user32" (ByVal hwnd As IntPtr,
ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr,
ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As
Integer
Const WM_CLOSE As Integer = &H10
Function GetWindowOwner(ByVal hwnd As IntPtr) As IntPtr
Return GetWindow(hwnd, GW_OWNER)
End Function
Function GetWindowCaption(ByVal hwnd As IntPtr) As String
Dim buffLen As Integer = GetWindowTextLength(hwnd)
Dim sb As New StringBuilder(buffLen + 1)
GetWindowText(hwnd, sb, sb.Capacity)
Return sb.ToString()
End Function
Function GetWindowClass(ByVal hwnd As IntPtr) As String
Dim sb As New StringBuilder(256)
GetClassName(hwnd, sb, sb.Capacity)
Return sb.ToString()
End Function
Private Function MyEnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam
As IntPtr) As Boolean
If IsWindowVisible(hWnd) AndAlso GetWindowOwner(hWnd).ToInt32() = 0
AndAlso GetWindowClass(hWnd) <> "Progman" Then
Dim caption As String = GetWindowCaption(hWnd)
If (caption.Length > 0) Then
ListBox1.Items.Add(caption)
End If
End If
Return True
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
EnumWindows(AddressOf MyEnumWindowsProc, IntPtr.Zero)
End Sub
Hope this helps. Please feel free to post here if anything is unclear.
Regards,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.