Results 1 to 3 of 3

Thread: open window captions

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    open window captions

    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?

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Try something like this:

    In a Module:
    VB Code:
    1. Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    2. 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
    3. Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
    4.  
    5. Private Const WM_GETTEXT = &HD
    6.  
    7. Private aCaptions() As String
    8. Private lCount As Long
    9.  
    10. Public Function GetAllCaptions() As Variant
    11.     lCount = 0
    12.     Call EnumWindows(AddressOf EnumWindowsProc, 0&)
    13.     If lCount Then ReDim Preserve aCaptions(lCount - 1)
    14.     GetAllCaptions = IIf(lCount, aCaptions, Array())
    15. End Function
    16.  
    17. Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    18.     Dim sBuffer As String * 260
    19.     If IsWindowVisible(hwnd) Then
    20.         ReDim Preserve aCaptions(lCount)
    21.         aCaptions(lCount) = Left(sBuffer, SendMessage(hwnd, WM_GETTEXT, 260, ByVal sBuffer))
    22.         If Len(Trim(aCaptions(lCount))) Then lCount = lCount + 1
    23.     End If
    24.     EnumWindowsProc = hwnd
    25. End Function
    Example Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim vCaps As Variant
    3.     Dim lIndex As Long
    4.    
    5.     vCaps = GetAllCaptions()
    6.     List1.Clear
    7.     For lIndex = 0 To UBound(vCaps)
    8.         List1.AddItem vCaps(lIndex)
    9.     Next
    10. End Sub

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width