Ok - try this
In a new project create a module - and put this code in it.
Code:
Option Explicit
Private Declare Function CallWindowProc 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_ACTIVATEAPP = &H1C
'm_frmHooked is the form that the subclass 'listens' in on
Private m_lPrevProc As Long, m_frmHooked As Form
Public Function WndProc(ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
'Let the window process messages as well
WndProc = CallWindowProc(m_lPrevProc, hWnd, wMsg, wParam, lParam)
'See what message has been sent to the window
If wMsg = WM_ACTIVATEAPP Then
'It's the activateapp message so call the sub on the form
Call m_frmHooked.AppFocus(CBool(wParam))
End If
End Function
Public Sub Unsubclass()
Debug.Print "UnSubClass"
Call SetWindowLong(m_frmHooked.hWnd, GWL_WNDPROC, m_lPrevProc)
Set m_frmHooked = Nothing
End Sub
Public Sub Subclass(ByRef r_frm As Form)
Debug.Print "SubClass"
Set m_frmHooked = r_frm
m_lPrevProc = SetWindowLong(r_frm.hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Now in the form itself - put this code.
Code:
Private Sub Form_Load()
Call Subclass(Me)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Unsubclass
End Sub
Public Sub AppFocus(ByVal v_bFocus As Boolean)
If v_bFocus Then
'App has just gained focus
Me.Caption = "ActivateApp; has-focus"
Else
'App has just lost focus
Me.Caption = "ActivateApp; no-focus"
End If
End Sub
You are probably best to create an .EXE to run this - as subclassing in the IDE is a bad idea.
Does this work for you??