Website Design United States, Website Design California, Website Designing United States, Website Designing California

Subclass your form and determine when your app has focus - Visual Basic 6

By subclassing (hooking) your own application, you can determine when your app gains and loses focus.

'Begin Code Block
'All code provided as is, by Red-Leif International.
'There is NO GUARANTEE that this code works. If you find that
'this code does not work, but you fix it so it now works, please
'email me the new code, and I will place it on my website.

'Eric :)
'Red-Leif International
Option Explicit

Private 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
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

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

Private glPrevWndProc As Long
Private glHWND As Long

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

Public Sub Unhook()
Dim lR As Long

'Reset the message handler for this window
lR = SetWindowLong(glHWND, GWL_WNDPROC, glPrevWndProc) '
End Sub

Private Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_ACTIVATEAPP Then 'Check for the ActivateApp message
If wParam <> 0 Then 'Check to see if Activating the application
Form1.Caption = "Focus Restored" 'Application Received Focus
Else '
Form1.Caption = "Focus Lost" 'Application Lost Focus
End If
End If
'Pass message on to the original window message handler
WindowProc = CallWindowProc(glPrevWndProc, hw, uMsg, wParam, lParam) '
End Function

Private Sub Form_Load()
glHWND = Form1.hwnd 'Store handle to this form's window
Hook 'Call procedure to begin capturing messages for this window
End Sub

Private Sub Form_Unload(Cancel As Integer)
Unhook 'Call procedure to stop intercepting the messages for this window
End Sub

42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Tel: 91-011-40502005, 9810067295



© 2008-2009