How can I get caption of all open windows??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to to get the caption of all open windows. Not only for my
application, but for all running programs.

I know it has something to do with Win32 EnumWindows, but I've tried some
examples, but can't make it work.

I need the sample code .... please! :o)

Thank you in advance!

M o j o
 
Hi,

Dim cb As CallBack

' VB.NET declaration
Public Delegate Function CallBack( _
ByVal hwnd As IntPtr, _
ByVal lParam As IntPtr) As Boolean

Public Declare Function EnumWindows Lib "user32" ( _
ByVal lpEnumFunc As CallBack, _
ByVal lParam As Integer) As Integer

Declare Function GetWindowTextLength Lib "user32" Alias
"GetWindowTextLengthA" ( _
ByVal hwnd As IntPtr) As Integer

Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As IntPtr, _
ByVal lpString As StringBuilder, _
ByVal cch As Integer) As Integer

Private Declare Function SetParent Lib "user32" (ByVal hWndChild As
IntPtr, _
ByVal hWndNewParent As IntPtr) As Integer

Public Function MyCallBack(ByVal hwnd As IntPtr, ByVal lParam As IntPtr)
As Boolean
Dim intLen As Integer = GetWindowTextLength(hwnd) + 1
If intLen > 1 Then
Dim strText As New StringBuilder(intLen)
Try
GetWindowText(hwnd, strText, intLen)
ListBox1.Items.Add(strText.ToString)
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
End If
Return True
End Function


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
cb = New CallBack(AddressOf MyCallBack)
EnumWindows(cb, 0)
End Sub

Ken
 
Hi Ken,

THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I've spent two days now searching for the answer, converting vb6 and cs
files without any luck.

Your solution works PERFECTLY! :o))))

Thanks once more!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Kind regards,
M o j o
 
Back
Top