Click to See Complete Forum and Search --> : open window captions
chrisjk
Aug 4th, 2001, 07:54 AM
Anyone got any ideas as to how I can get the caption of all the open windows (be they Notepad, IE, Explorer, Calc etc etc including multiple instances of the same app) and display them in a list box?
Aaron Young
Aug 4th, 2001, 08:17 AM
Try something like this:
In a Module:Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Const WM_GETTEXT = &HD
Private aCaptions() As String
Private lCount As Long
Public Function GetAllCaptions() As Variant
lCount = 0
Call EnumWindows(AddressOf EnumWindowsProc, 0&)
If lCount Then ReDim Preserve aCaptions(lCount - 1)
GetAllCaptions = IIf(lCount, aCaptions, Array())
End Function
Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sBuffer As String * 260
If IsWindowVisible(hwnd) Then
ReDim Preserve aCaptions(lCount)
aCaptions(lCount) = Left(sBuffer, SendMessage(hwnd, WM_GETTEXT, 260, ByVal sBuffer))
If Len(Trim(aCaptions(lCount))) Then lCount = lCount + 1
End If
EnumWindowsProc = hwnd
End FunctionExample Usage:Private Sub Command1_Click()
Dim vCaps As Variant
Dim lIndex As Long
vCaps = GetAllCaptions()
List1.Clear
For lIndex = 0 To UBound(vCaps)
List1.AddItem vCaps(lIndex)
Next
End Sub
chrisjk
Aug 4th, 2001, 08:22 AM
Excellent, thanks! ;)
I had to change the Private's to Public so I could put it in a module (otherwise I get AddressOf error), but works well, nice one :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.