Allows you to perform processing when your application loses or gains focus.
Option Explicit 'The original wndproc of our form Dim PrevProc As Long 'The message that we're catching Public Const WM_ACTIVATEAPP = &H1C Declare Function GetWindowLong Lib "user32" Alias _ "GetWindowLongA" (ByVal hWnd As Long, _ ByVal nIndex As Long) As Long Declare Function SetWindowLong Lib "user32" Alias _ "SetWindowLongA" (ByVal hWnd As Long, _ ByVal nIndex As Long, ByVal dwNewLong As Long) _ As Long Public Const GWL_WNDPROC = (-4) 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 Public Sub Subclass(hWnd As Long) PrevProc = GetWindowLong(hWnd, GWL_WNDPROC) SetWindowLong hWnd, GWL_WNDPROC, AddressOf AppActWndProc End Sub Public Sub UnSubclass(hWnd As Long) SetWindowLong hWnd, GWL_WNDPROC, PrevProc PrevProc = 0 End Sub Public Function AppActWndProc(ByVal hWnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, _ ByVal lParam As Long) As Long 'Check to see if our application's focus 'status has changed. If wMsg = WM_ACTIVATEAPP Then If wParam = 0 Then 'DEACTIVATE CODE HERE frmTest.lst.AddItem "Deactivated - " & _ "Thread Getting Focus: " & lParam Else 'ACTIVATE CODE HERE frmTest.lst.AddItem "Activated - " & _ "Thread Losing Focus: " & lParam End If End If AppActWndProc = CallWindowProc(PrevProc, hWnd, _ wMsg, wParam, lParam) End Function 'In your form . . . 'This example only requires one control: Drop a ListBox onto the form and 'give it the distinguishing name of lst. 'Now we'll add the code to the form: Option Explicit Private Sub Form_Load() 'Subclass to get into the form's WndProc Subclass hWnd End Sub Private Sub Form_Unload(Cancel As Integer) 'Unhook our procedure from the form UnSubclass hWnd End Sub
