Note: In order to test the code in this article, you will need the
AddressOf code as well.
By subclassing a form, we can find out when it loses or gains the
active window status.
Create a popup form frmAppStatus
and place a label lblStatus on it. From it's OnOpen and OnUnload event,
call the Hook and UnHook subroutines as
Private Sub Form_Open(Cancel as Integer)
Call sHook(Me.hWnd, "fWatchActiveStatus")
End Sub
Private Sub Form_Unload(Cancel as Integer)
Call sUnHook(Me.hWnd)
End Sub
Place this code in a new module and compile and Save all Modules.
Private Declare Function apiCallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal Hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long
Private Declare Function apiSetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal Hwnd As Long, _
ByVal nIndex As Long, _
ByVal wNewWord As Long) _
As Long
Private Declare Function apiGetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal Hwnd As Long, _
ByVal nIndex As Long) _
As Long
Private lpPrevWndProc As Long
Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_ACTIVATEAPP = &H1C
Function fWatchActiveStatus( _
ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long
On Error Resume Next
If uMsg = WM_ACTIVATEAPP Then
If wParam <> 0 Then
Forms!frmAppStatus!lblStatus.Caption = _
"Woohoo! I have the focus again! Thanks!!"
Else
Forms!frmAppStatus!lblStatus.Caption = _
"Whaasamatta??? You don't like me no more??"
End If
End If
fWatchActiveStatus = apiCallWindowProc( _
ByVal lpPrevWndProc, _
ByVal hw, _
ByVal uMsg, _
ByVal wParam, _
ByVal lParam)
End Function
|