VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Correct way to determine when your visual basic application gains or loses focus.

by Anonymous (267 Submissions)
Category: Windows System Services
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 22nd December 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Correct way to determine when your visual basic application gains or loses focus.

API Declarations



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

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Public Const WM_ACTIVATEAPP = &H1C
Public Const GWL_WNDPROC = -4

Global lpPrevWndProc As Long
Global gHW As Long

Rate Correct way to determine when your visual basic application gains or loses focus.



   'Store handle to this form's window
    gHW = Me.hWnd

   'Call procedure to begin capturing messages for this window
    Hook
End Sub

'in VB6 on this can be placed in Form_Terminate
Private Sub Form_Unload(Cancel As Integer)
   'Call procedure to stop intercepting the messages for this window
    Unhook
End Sub
 




Public Sub Hook()
   'Establish a hook to capture messages to this window
    lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
    AddressOf WindowProc)
End Sub

Public Sub Unhook()
    Dim temp As Long

   'Reset the message handler for this window
    temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long

     'Check for the ActivateApp message
      If uMsg = WM_ACTIVATEAPP Then
        'Check to see if Activating the application
         If wParam <> 0 Then
           'Application Received Focus
            Form1.Caption = "Focus Restored"
          Else
           'Application Lost Focus
            Form1.Caption = "Focus Lost"
         End If
      End If

     'Pass message on to the original window message handler
      WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, _
           lParam)
End Function


Download this snippet    Add to My Saved Code

Correct way to determine when your visual basic application gains or loses focus. Comments

No comments have been posted about Correct way to determine when your visual basic application gains or loses focus.. Why not be the first to post a comment about Correct way to determine when your visual basic application gains or loses focus..

Post your comment

Subject:
Message:
0/1000 characters