-
I need to have an event occur whenever someone multitasks to another program window. I thought that a move off of the program window would throw this:
Private Sub Form_LostFocus()
MsgBox "selected another window", vbOKOnly
End Sub
But apparently not... Any ideas?
-
Will not fire when the focus shifts to another prog outside of Visual Basic.
-
Might try Form_Deactivate
-
Nope
Nope
Private Sub Form_Deactivate()
MsgBox "selected another window", vbOKOnly
End Sub
Doesn't work either, any other ideas?
-
I'm going to guess and say that a modal form attached to a specific hWnd (ie: a message box), may not display the the app or form loses focus. Does it worl using Debug.Print statements?
- gaffa
-
hummm... I think I see what your saying.... but no the debug.print doesn't work either
-
Seems like you may have to subclass the window and catch the lost focus message.
-
FOCUS_EVENT_RECORD ??
perhaps, but that coud be fairly messy with this specific program, I just consulted my win 32 api's and found this gem, does this look like what I'm going for here?
-
You must subclass to detect the Lost_Focus of a form.
Code:
Option Explicit
Public Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal _
ndx As Long, ByVal newValue As Long) As Long
Public 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
' This is used with the SetWindowLong API function.
Public Const GWL_WNDPROC = -4
Public Const WM_KILLFOCUS = &H8
Dim saveHWnd As Long ' The handle of the subclassed window.
Dim oldProcAddr As Long ' The address of the original window procedure
Sub StartSubclassing(ByVal hWnd As Long)
saveHWnd = hWnd
oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Sub StopSubclassing()
SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
End Sub
Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
' Send the message to the original window procedure, and then
' return Windows the return value from the original procedure.
WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
Select Case uMsg
Case WM_KILLFOCUS
Debug.Print "Form has lost focus"
End Select
End Function
Private Sub Form_Load()
StartSubclassing Me.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
StopSubclassing
End Sub
-