Public Class Form1
' Derived from code at:
' http://www.thescarms.com/vbasic/alttab.aspx
Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As CallBack, ByVal lParam As Integer) As Integer
Declare Function GetForegroundWindow Lib "user32" () As Integer
Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
Declare Function GetWindowInteger Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Declare Function IsIconic Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Integer) As Integer
'
' Constants used with APIs
'
Public 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 Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Public Shared Function fEnumWindowsCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Dim lReturn As Integer
Dim lExStyle As Integer
Dim bNoOwner As Boolean
Dim sWindowText As String
'
' This callback function is called by Windows (from
' the EnumWindows API call) for EVERY window that exists.
' It populates the listbox with a list of windows that we
' are interested in.
'
' Windows to display are those that:
' - are not this app's
' - are visible
' - do not have a parent
' - have no owner and are not Tool windows OR
' have an owner and are App windows
'
If hwnd <> Form1.Handle Then
If IsWindowVisible(hwnd) Then
If GetParent(hwnd) = 0 Then
bNoOwner = (GetWindow(hwnd, GW_OWNER) = 0)
lExStyle = GetWindowInteger(hwnd, GWL_EXSTYLE)
If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or _
((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then
'
' Get the window's caption.
'
sWindowText = Space$(256)
lReturn = GetWindowText(hwnd, sWindowText, Len(sWindowText))
If lReturn Then
'
' Add it to our list.
'
sWindowText = Trim(sWindowText)
Form1.ListBox1.Items.Add(sWindowText)
End If
End If
End If
End If
End If
fEnumWindowsCallBack = 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 fEnumWindowsCallBack, 0)
End Sub
End Class
' Derived from code at:
' http://www.thescarms.com/vbasic/alttab.aspx
Declare Function EnumWindows Lib "user32" _
(ByVal lpEnumFunc As CallBack, ByVal lParam As Integer) As Integer
Declare Function GetForegroundWindow Lib "user32" () As Integer
Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
Declare Function GetWindowInteger Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Declare Function IsIconic Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Integer) As Integer
'
' Constants used with APIs
'
Public 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 Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Public Shared Function fEnumWindowsCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Dim lReturn As Integer
Dim lExStyle As Integer
Dim bNoOwner As Boolean
Dim sWindowText As String
'
' This callback function is called by Windows (from
' the EnumWindows API call) for EVERY window that exists.
' It populates the listbox with a list of windows that we
' are interested in.
'
' Windows to display are those that:
' - are not this app's
' - are visible
' - do not have a parent
' - have no owner and are not Tool windows OR
' have an owner and are App windows
'
If hwnd <> Form1.Handle Then
If IsWindowVisible(hwnd) Then
If GetParent(hwnd) = 0 Then
bNoOwner = (GetWindow(hwnd, GW_OWNER) = 0)
lExStyle = GetWindowInteger(hwnd, GWL_EXSTYLE)
If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or _
((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then
'
' Get the window's caption.
'
sWindowText = Space$(256)
lReturn = GetWindowText(hwnd, sWindowText, Len(sWindowText))
If lReturn Then
'
' Add it to our list.
'
sWindowText = Trim(sWindowText)
Form1.ListBox1.Items.Add(sWindowText)
End If
End If
End If
End If
End If
fEnumWindowsCallBack = 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 fEnumWindowsCallBack, 0)
End Sub
End Class
Comments