Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

API: Detect Application Activate/Deactivate

Author(s)
Dev Ashish

Note: In order to test the code in this article, you will need the AddressOf code as well.

    By subclassing a form, we can find out when it loses or gains the active window status.

    Create a popup form frmAppStatus and place a label lblStatus on it. From it's OnOpen and OnUnload event, call the Hook and UnHook subroutines as

'********** Code Start **********
Private Sub Form_Open(Cancel as Integer)
	Call sHook(Me.hWnd, "fWatchActiveStatus")
End Sub

Private Sub Form_Unload(Cancel as Integer)
	Call sUnHook(Me.hWnd)
End Sub
'********** Code End **********

Place this code in a new module and compile and Save all Modules.

'*********** Code Start ************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiCallWindowProc 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
   
Private Declare Function apiSetWindowLong Lib "user32" _
    Alias "SetWindowLongA" _
    (ByVal Hwnd As Long, _
    ByVal nIndex As Long, _
    ByVal wNewWord As Long) _
    As Long

Private Declare Function apiGetWindowLong Lib "user32" _
    Alias "GetWindowLongA" _
   (ByVal Hwnd As Long, _
   ByVal nIndex As Long) _
   As Long


Private lpPrevWndProc  As Long
Private Const GWL_WNDPROC  As Long = (-4)
Private Const WM_ACTIVATEAPP = &H1C

Function fWatchActiveStatus( _
                ByVal hw As Long, _
                ByVal uMsg As Long, _
                ByVal wParam As Long, _
                ByVal lParam As Long) _
                As Long
                
    On Error Resume Next
    If uMsg = WM_ACTIVATEAPP Then
        If wParam <> 0 Then
            Forms!frmAppStatus!lblStatus.Caption = _
                    "Woohoo!  I have the focus again!  Thanks!!"
        Else
            Forms!frmAppStatus!lblStatus.Caption = _
                    "Whaasamatta???  You don't like me no more??"
        End If
    End If
    fWatchActiveStatus = apiCallWindowProc( _
                            ByVal lpPrevWndProc, _
                            ByVal hw, _
                            ByVal uMsg, _
                            ByVal wParam, _
                            ByVal lParam)
End Function
'***************** Code End ***************

© 1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer