For a form, you will have to have two forms and click one and the other will lose the focus. But what you probably want to do is know when your program has lost focus. You can subclass to do this.
Can't remember who this code is from.
It's located somewhere on Vb-World.
Great code to whoever gave it!
Code:Option Explicit Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long 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 ' This is used with the SetWindowLong API function. 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 'Form has Lost Focus Form1.WindowState = 1 End Select End Function Private Sub Form_Load() StartSubclassing Me.hWnd End Sub Private Sub Form_Unload(Cancel As Integer) StopSubclassing End Sub





Reply With Quote