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.
Printable View
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.
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
How would you return the "title" of this window using the returned handle from the GetForegroundWindow?
GetWindowText, I'm pretty sure, or GetWindowTitle.
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