1.

Solve : Get open windows vb?

Answer»

I need help. please tell me an api to get all open windows. it is urgent(not really)

EnumWindows()i use vb 2008 express
and i really dont have xp i got a new computerQuote from: glaba on April 28, 2010, 04:45:12 PM

i use vb 2008 express
and i really dont have xp i got a new computer
Quote from: BC_Programmer on April 28, 2010, 04:43:13 PM
EnumWindows()
it does NOT workQuote from: glaba on April 28, 2010, 04:49:30 PM
it does NOT work


works fine for me.
Code: [Select]Module Module1
Delegate Function EnumWindowsProc(ByVal HWND As Integer, ByVal lParam As Integer) As Integer
PRIVATE Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32
Private Declare Function GetWindowTextAPI Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32

Private mPendingList As List(Of Integer)
Private Function EnumWnd(ByVal hWnd As Integer, ByVal lParam As Integer)
mPendingList.Add(hWnd)
Return 1
End Function
Public Function GetTopLevelWindows() As List(Of Integer)
mPendingList = New List(Of Integer)
CALL EnumWindows(New EnumWindowsProc(AddressOf EnumWnd), 0)

Return mPendingList


End Function
Private Function GetWindowText(ByVal hwnd As Int32) As String
Dim retstring As String = Space$(32768)
GetWindowTextAPI(hwnd, retstring, Len(retstring))
Return retstring.Replace(vbNullChar, "").Trim()
End Function
Sub MAIN()
Dim windowhandles As List(Of Integer) = GetTopLevelWindows()
Dim currtitle As String
For Each currhandle As Integer In windowhandles
currtitle = GetWindowText(currhandle)
If currtitle.Length > 0 Then
Console.WriteLine(currtitle)
Debug.Print(currtitle)
End If

Next
End Sub

End Module






Edited- added FindWindowsLike() and FindWindowsLikeRegExp()...

Code: [Select]Imports System.Text.RegularExpressions

Module Module1

Delegate Function EnumWindowsProc(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer
Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32
Private Declare Function GetWindowTextAPI Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32
Private Declare Function GetParent Lib "user32.dll" (ByVal hwnd As Int32) As Int32
Private Declare Function GetDesktopWindow Lib "user32.dll" () As Int32

Private mPendingList As List(Of Integer)
Private Function EnumWnd(ByVal hWnd As Integer, ByVal lParam As Integer)
If lParam <> 0 Then
If IsTopLevelWindow(hWnd) Then
mPendingList.Add(hWnd)
End If
Else
mPendingList.Add(hWnd)
End If
Return 1
End Function
Private Function IsTopLevelWindow(ByVal HwndTest As Long) As Boolean
Return GetParent(HwndTest) = 0 OrElse GetParent(HwndTest) = GetDesktopWindow()
End Function
Public Function GetTopLevelWindows(Optional ByVal OnlyTopMost As Boolean = True) As List(Of Integer)
mPendingList = New List(Of Integer)
Call EnumWindows(New EnumWindowsProc(AddressOf EnumWnd), OnlyTopMost)




Return mPendingList


End Function
Private Function GetWindowText(ByVal hwnd As Int32) As String
Dim retstring As String = Space$(32768)
GetWindowTextAPI(hwnd, retstring, Len(retstring))
Return retstring.Replace(vbNullChar, "").Trim()
End Function
Public Function FindWindowsLike(ByVal LikeString As String, Optional ByVal OnlyTopMost As Boolean = False) As List(Of String)
Dim foundwindows As List(Of Integer) = GetTopLevelWindows(OnlyTopMost)
Dim loopwindow As Int32, wndtext As String

Dim returntitles As New List(Of String)


For Each loopwindow In foundwindows
wndtext = GetWindowText(loopwindow)

If wndtext Like LikeString Then
returntitles.Add(wndtext)
End If
Next


Return returntitles

End Function
Public Function FindWindowsLikeRegExp(ByVal LikeString As String, Optional ByVal regexoptions As RegexOptions = Text.RegularExpressions.RegexOptions.IgnoreCase, _
Optional ByVal OnlyTopMost As Boolean = False) As List(Of String)
Dim foundwindows As List(Of Integer) = GetTopLevelWindows(OnlyTopMost)
Dim loopwindow As Int32, wndtext As String
Dim usereg As Regex
Dim returntitles As New List(Of String)
usereg = New Regex(LikeString)

For Each loopwindow In foundwindows
wndtext = GetWindowText(loopwindow)
If usereg.Matches(wndtext).Count > 0 Then
returntitles.Add(wndtext)
End If
Next


Return returntitles

End Function

Sub Main()
Dim windowhandles As List(Of Integer) = GetTopLevelWindows(True)
Dim currtitle As String
Console.WriteLine("Enumerating all top-level windows...")
For Each currhandle As Integer In windowhandles
currtitle = GetWindowText(currhandle)
If currtitle.Length > 0 Then
Console.WriteLine(currtitle)
Debug.Print(currtitle)
End If

Next

Console.WriteLine("finding windows like ""A*""...")
Dim windowslike As List(Of String) = FindWindowsLike("A*", True)
Console.WriteLine("Found " + Str(windowslike.Count) + " matching items...")
For Each LoopTitle As String In windowslike
Debug.Print(LoopTitle)
Console.WriteLine(LoopTitle)



Next
End Sub

End Module



Discussion

No Comment Found