Results 1 to 4 of 4

Thread: What program has focus?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313

    Question

    Say I have word, excel, and Microsoft internet explorer running, how can i check to see which one has the focus?

  2. #2
    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
    
    ' Returns the title of the active window.
    '    if GetParent = true then the parent window is
    '                   returned.
    Public 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
    
    
    Public 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
    
    
    Usage:
    
    MsgBox GetActiveWindowTitle(True)

  3. #3
    Guest

    Lightbulb Try This:

    Try:
    Code:
    Public Declare Function GetActiveWindow Lib "user32" () As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Public Textx as String
    
    Public Function GetActiveWinText() as String
        dim xc as long, xl as string
        xc = GetActiveWindow()
        xl = GetWindowText(xc, Textx, 10000)
        '10000 means return a maxium of 10000 chars, which you can reset to your likes.
        GetActiveText = Textx
    End Function
    
    Public Function GetActiveHandle() as Long
        GetActiveHandle = GetActiveWindow()
    End Function
    Usage:
    Code:
    'To get window's Title Bar Text:
    var = GetActiveWinText()
    'to get window's handle:
    var = GetActiveWindow()

  4. #4
    Guest
    Pardon Moi, Matthew, I didn't notice that you posted...

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