Prev: dhSqliteDemo
Next: Scientific to Decimal
From: mayayana on 1 Dec 2009 11:50 Here's a sample of SetWinEventHook. I don't know what issues might be involved with a server-hosted program. You'd need to test that. It seems to work fine for me testing it locally. Basically this sets up a system-wide hook that informs you when the focused window is changing. The callback includes the hWnd of the window involved, so you can easily tell whether your window is involved. You can actually get event notification for specific ranges of events. The first two parameters of the function give the lower and upper range of constant value to monitor. By setting both values to SYS_FOREGROUND you should only be informed when the foreground window changes. One note: Don't do anything like put a msgbox in the event sub. That could catch you in a loop of focus - msgbox - focus -msgbox - etc. Code in form named F1: '---------------------------------------- Friend Sub GettingFocus(IfFocused As Boolean) '-- F1 has a label named Lab1 Lab1.Caption = CStr(IfFocused) End Sub Private Sub Form_Load() StartEventHook End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) StopEventHook End Sub '----------------end form code ----------- '------- code in .bas module --------------- Public Const SYS_FOREGROUND = 3& Public Declare Function SetWinEventHook Lib "user32.dll" (ByVal eventMin As Long, ByVal eventMax As Long, ByVal hmodWinEventProc As Long, ByVal pfnWinEventProc As Long, ByVal idProcess As Long, ByVal idThread As Long, ByVal dwFlags As Long) As Long Public Declare Function UnhookWinEvent Lib "user32.dll" (ByVal LHandle As Long) As Long Public LHook As Long Public Sub StartEventHook() LHook = SetWinEventHook(SYS_FOREGROUND, SYS_FOREGROUND, 0&, AddressOf WinEventFunc, 0, 0, 0) End Sub Public Sub StopEventHook() Dim LRet As Long If LHook = 0 Then Exit Sub LRet = UnhookWinEvent(LHook) End Sub Public Function WinEventFunc(ByVal HookHandle As Long, ByVal LEvent As Long, ByVal hWnd As Long, ByVal idObject As Long, ByVal idChild As Long, ByVal idEventThread As Long, ByVal dwmsEventTime As Long) As Long If hWnd = F1.hWnd Then F1.GettingFocus True Else F1.GettingFocus False End If End Function '---------end .bas code ------------------ |