Results 1 to 2 of 2

Thread: Getting the correct caption from window

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Question Getting the correct caption from window

    Hey All,

    I'm using the following code to get the classname, and window text...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) _
    4. As Long
    5. Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" _
    6. (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    7. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) _
    8. As Long
    9. Private Declare Function GetWindowLong Lib "user32" Alias _
    10. "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) _
    11. As Long
    12. Private Declare Function IsWindowVisible Lib "user32" _
    13. (ByVal hwnd As Long) As Long
    14. Private Declare Function GetWindowText Lib "user32" Alias _
    15. "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
    16. ByVal cch As Long) As Long
    17. Private Declare Function GetClassName Lib "user32" Alias _
    18. "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, _
    19. ByVal nMaxCount As Long) As Long
    20.  
    21. Private Const GW_HWNDFIRST = 0
    22. Private Const GW_HWNDNEXT = 2
    23. Private Const GWL_STYLE = (-16)
    24. Private Const GWL_HWNDPARENT = (-8)
    25. Private Const WS_OVERLAPPED = &H0&
    26. Private Const WS_BORDER = &H800000
    27. Private Const WS_DLGFRAME = &H400000
    28. Private Const WS_CAPTION = WS_BORDER Or WS_DLGFRAME
    29. Private Const WS_SYSMENU = &H80000
    30. Private Const WS_THICKFRAME = &H40000
    31. Private Const WS_MINIMIZEBOX = &H20000
    32. Private Const WS_MAXIMIZEBOX = &H10000
    33. Private Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or _
    34. WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or _
    35. WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
    36.  
    37. ' Return information about this window.
    38. Private Sub GetWindowInfo(ByVal app_hwnd As Long, _
    39. ByRef app_parent As Long, ByRef app_owner As Long, _
    40. ByRef app_visible As Boolean, ByRef app_style As Long, _
    41. ByRef app_text As String, ByRef app_class As String)
    42. Const MAX_LENGTH = 1024
    43.  
    44. Dim buf As String * MAX_LENGTH
    45. Dim length As Long
    46.  
    47.     app_parent = GetParent(app_hwnd)
    48.     app_owner = GetWindowLong(app_hwnd, GWL_HWNDPARENT)
    49.     app_visible = IsWindowVisible(app_hwnd)
    50.     app_style = GetWindowLong(app_hwnd, GWL_STYLE)
    51.  
    52.     length = GetWindowText(app_hwnd, buf, MAX_LENGTH)
    53.     app_text = Left$(buf, length)
    54.  
    55.     length = GetClassName(app_hwnd, buf, MAX_LENGTH)
    56.     app_class = Left$(buf, length)
    57. End Sub
    58.  
    59. Private Sub Form_Load()
    60. Dim app_hwnd As Long
    61. Dim app_parent As Long
    62. Dim app_owner As Long
    63. Dim app_visible As Boolean
    64. Dim app_style As Long
    65. Dim app_text As String
    66. Dim app_class As String
    67.  
    68.     app_hwnd = GetTopWindow(0)
    69.  
    70.     Do While app_hwnd <> 0
    71.         ' Get information about this window.
    72.         GetWindowInfo app_hwnd, app_parent, app_owner, _
    73.             app_visible, app_style, app_text, app_class
    74.  
    75.         ' See if this window is interesting.
    76.         If app_visible And _
    77.             app_parent = 0 And _
    78.             app_owner = 0 And _
    79.             Len(app_text) > 0 And _
    80.             Left$(app_text, 8) <> "VBBubble" And _
    81.             (Left$(app_class, 7) <> "Progman" Or _
    82.             (app_style And WS_OVERLAPPEDWINDOW) <> 0) _
    83.         Then
    84.             List1.AddItem app_class & "   -   " & app_text
    85.         End If
    86.  
    87.         app_hwnd = GetNextWindow(app_hwnd, GW_HWNDNEXT)
    88.     Loop
    89.  
    90. End Sub

    The problem is when it returns a VB app's information, the text is not the
    window caption. It is the programs title.

    In other words, if you were to start a new project in VB, and then run this
    code, it would return...

    ThunderRT5Main - Project1

    instead of...

    ThunderRT5Main - Form1


    Could someone please help me figure out how to get the window caption?
    This is driving me nuts!

    Thanks in advance,
    Ron

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Getting the correct caption from window

    I did find this code, and it works... so long as the app is the active window.

    I just don't know how to combine the two.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetActiveWindow Lib "user32" () As Long
    4. Private Declare Function GetWindowText Lib "user32" Alias _
    5. "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
    6. ByVal cch As Long) As Long
    7. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    8.  
    9. Private Sub Timer1_Timer()
    10. Dim foreground_hwnd As Long
    11. Dim txt As String
    12. Dim length As Long
    13.  
    14.     foreground_hwnd = GetForegroundWindow()
    15.  
    16.     txt = Space$(1024)
    17.     length = GetWindowText(foreground_hwnd, txt, Len(txt))
    18.     txt = Left$(txt, length)
    19.  
    20.     Label1.Caption = txt
    21. End Sub

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