Results 1 to 5 of 5

Thread: Active Application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    93
    Is there a way to find out the active application? The reason for this is I use the "AppActivate" method in VB, however I the application I activate is already active, my program freezes.

  2. #2
    Megatron
    Guest
    Use the GetForegroundWindow function.
    Code:
    Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long
    
    Private Sub Command1_Click
        MsgBox "The handle of the active window is: " & GetForegroundWindow
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    93
    How would you return the "title" of this window using the returned handle from the GetForegroundWindow?

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    GetWindowText, I'm pretty sure, or GetWindowTitle.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    Matthew Gates
    Guest
    Use this code.


    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