Results 1 to 2 of 2

Thread: VB6 event for capturing the active document

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    52

    VB6 event for capturing the active document

    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?

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,229

    Re: VB6 event for capturing the active document

    no, but you can set a hook.

    EVENT_SYSTEM_FOREGROUND would be what you're listening for.

    Module1.bas
    Code:
    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
    Form1.frm with a ListBox1
    Code:
    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
    translated from Raymond Chen's blog
    Last edited by DEXWERX; Jun 30th, 2016 at 11:10 AM. Reason: added an example

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width