'
' Declarations to handle "What was the last app used?"
'
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As Long, ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public thisHwnd As Long
Public LastApp As Long
Public LastAppTitle As String
Public LastAppClass As String
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Const WM_CLOSE = &H10
Public Const INFINITE = &HFFFFFFFF
'
' Now some code to use them
'
Public Function WhoCalledUs()
thisHwnd = GetActiveWindow()
LastApp = 0
Call EnumWindows(AddressOf EnumWinProc, 0)
Select Case LastAppClass
Case "OpusApp"
WhoCalledUs = "Word"
Case "XLMAIN"
WhoCalledUs = "Excel"
Case "rctrl_renwnd32"
WhoCalledUs = "Outlook"
Case "ExploreWClass"
WhoCalledUs = "WinExp"
Case "PP9FrameClass"
WhoCalledUs = "PPT"
Case Else
WhoCalledUs = ""
End Select
End Function
Public Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim lRet, lWindowStyle, lWindowParent As Long
Dim strBuffer As String
'Only keep windows that are visible,
' don't have parents, have a title bar and a caption,
' and are not this application, and are the first application found
If hwnd <> thisHwnd Then ' Not this app
If IsWindowVisible(hwnd) Then ' Window visible
lWindowParent = GetParent(hwnd)
If lWindowParent = 0 Then ' No parents
lWindowStyle = GetWindowLong(hwnd, GWL_STYLE)
If (lWindowStyle And WS_CAPTION) Then ' Has a caption
strBuffer = Space(MAX_LEN)
lRet = GetWindowText(hwnd, strBuffer, Len(strBuffer))
If lRet Then ' Has a title
If LastApp = 0 Then
LastApp = hwnd
LastAppTitle = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
strBuffer = Space(MAX_LEN)
lRet = GetClassName(hwnd, strBuffer, MAX_LEN)
LastAppClass = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
End If
End If
End If
End If
End If
End If
EnumWinProc = 1
End Function