The question:
I am trying to list all of the currently open windows that are in the taskbar. Does anybody know how to do that?
Thanks in advance.
The accepted answer by Tall Dude:
If you just want the applications,
(not all windows,) try:
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
3 comments:
This is the first time in my carear of developer that I find an example of code that effectively compiles after a copy-paste, without having to modify one single line at all. Impressive. Thanks for that code.
I have been looking for a 'list running applications' in VB.NET for some time now. None seem to work or they are in VB6. This ran first time! Many many thanks indeed.
AGO
Thanks so much!! I have looked EVERYWHERE for a simple answer to this question, and this works perfectly! Thanks again.
Post a Comment