During the session many PowerPoint file may be open. While my prog run and a form is on, the user may want to change the active file from the taskbar.
Is there an event that can capture the change?
Printable View
During the session many PowerPoint file may be open. While my prog run and a form is on, the user may want to change the active file from the taskbar.
Is there an event that can capture the change?
no, but you can set a hook.
EVENT_SYSTEM_FOREGROUND would be what you're listening for.
Module1.bas
Form1.frm with a ListBox1Code:Option Explicit
Public Const OBJID_WINDOW As Long = &H0
Public Const CHILDID_SELF As Long = 0&
Public Const EVENT_SYSTEM_FOREGROUND As Long = &H3&
Public Const WINEVENT_OUTOFCONTEXT As Long = &H0&
Public Const WINEVENT_SKIPOWNPROCESS As Long = &H2&
Public Const NULL_ As Long = 0&
Public Declare Function SetWinEventHook Lib "user32" (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" (ByVal hWinEventHook As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextW" (ByVal hWnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
Public Declare Function IsWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Sub WinEventProc( _
ByVal hWinEventHook As Long, _
ByVal dwEvent As Long, _
ByVal hWnd As Long, _
ByVal idObject As Long, _
ByVal idChild As Long, _
ByVal dwEventThread As Long, _
ByVal dwmsEventTime As Long)
If (hWnd And CBool(IsWindow(hWnd)) And _
idObject = OBJID_WINDOW And _
idChild = CHILDID_SELF) Then
Dim Buf$: Buf = String$(200, vbNullChar)
Form1.List1.AddItem Left$(Buf, GetWindowText(hWnd, StrPtr(Buf), Len(Buf)))
End If
End Sub
translated from Raymond Chen's blogCode:Option Explicit
Private hWinEventHook As Long
Private Sub Form_Load()
hWinEventHook = SetWinEventHook( _
EVENT_SYSTEM_FOREGROUND, _
EVENT_SYSTEM_FOREGROUND, _
NULL_, AddressOf WinEventProc, 0, 0, _
WINEVENT_OUTOFCONTEXT Or WINEVENT_SKIPOWNPROCESS)
End Sub
Private Sub Form_Unload(Cancel As Integer)
If hWinEventHook Then UnhookWinEvent hWinEventHook
End Sub