By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

How can I get caption of all open windows??

P: n/a
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
Mar 31 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
----------------
"M O J O" <MO**@discussions.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
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

Mar 31 '06 #2

P: n/a
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

"Ken Tucker [MVP]" wrote:
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
----------------
"M O J O" <MO**@discussions.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
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


Mar 31 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.