Results 1 to 3 of 3

Thread: title of the window in focus

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Maine
    Posts
    5

    Question title of the window in focus

    I am looking for a way to get the title of the window currently in focus. I should know how to do this but I've been coding forever and it's late.

    I hope someone can help, thanks
    ~* Sp1Ral FlAmE *~ & Intrix Software

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    To get the caption of a window:

    sWindowText = String(255, Chr(0))
    SendMessageByStr hWnd, WM_GETTEXT, 255, sWindowText
    sWindowText = Left(sWindowText, InStr(1, sWindowText, Chr(0), vbTextCompare) - 1)]


    To get window current window:
    GetForegroundWindow()

    I assume you don't need the declarations.

  3. #3
    Matthew Gates
    Guest
    Try this:


    Code:
    Private Declare Function GetForegroundWindow _
    Lib "user32" () As Long
    
    Private Declare Function GetWindowTextLength _
    Lib "user32" Alias "GetWindowTextLengthA" (ByVal _
    hwnd As Long) As Long
    
    Private Declare Function GetWindowText Lib "user32" _
    Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal _
    lpString As String, ByVal cch As Long) As Long
    
    Private Declare Function GetParent Lib "user32" (ByVal _
    hwnd As Long) As Long
    
    
    Private Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String
       Dim i As Long
       Dim j As Long
    
       i = GetForegroundWindow
    
    
       If ReturnParent Then
          Do While i <> 0
             j = i
             i = GetParent(i)
          Loop
    
          i = j
       End If
    
       GetActiveWindowTitle = GetWindowTitle(i)
    End Function
    
    
    Private Function GetWindowTitle(ByVal hwnd As Long) As String
       Dim l As Long
       Dim s As String
    
       l = GetWindowTextLength(hwnd)
       s = Space(l + 1)
    
       GetWindowText hwnd, s, l + 1
    
       GetWindowTitle = Left$(s, l)
    End Function

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