Hey everbody...I'm just sharing some code I wrote to answer a recent question.
I couldn't find any VB6 examples here on EE of a "Shell Hook" via RegisterShellHookWindow():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/registershellhookwindow.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/ShellProc.aspThis example shows what window last had the focus just before your app was activated.
Create a New Project and add a Module. Paste the code below into the appropriate areas.
*** Always close the app by clicking on the "X" in the top right of the form! ***
Points will be split among those who make comments...whether they be constructive or not. Let me know if the code is useful or complete rubbish.
~IM
' --------------------------
----
' Form1
' --------------------------
----
Option Explicit
Private Sub Form_Load()
Me.AutoRedraw = True
OldProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
uMsgNotify = RegisterWindowMessage("SHE
LLHOOK")
Call
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call DeregisterShellHookWindow(
Me.hWnd)
SetWindowLong hWnd, GWL_WNDPROC, OldProc
End Sub
' --------------------------
----
' Module1
' --------------------------
----
Option Explicit
Public Enum ShellEvents
HSHELL_WINDOWCREATED = 1
HSHELL_WINDOWDESTROYED = 2
HSHELL_ACTIVATESHELLWINDOW
= 3
HSHELL_WINDOWACTIVATED = 4
HSHELL_GETMINRECT = 5
HSHELL_REDRAW = 6
HSHELL_TASKMAN = 7
HSHELL_LANGUAGE = 8
HSHELL_ACCESSIBILITYSTATE = 11
End Enum
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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
Public Declare Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public OldProc As Long
Public uMsgNotify As Long
Public lastWnd As Long
Public Function WndProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim curWnd As Long
Select Case wMsg
Case uMsgNotify
Select Case wParam
Case ShellEvents.HSHELL_WINDOWA
CTIVATED
curWnd = GetForegroundWindow()
If curWnd <> Form1.hWnd Then
' store the last activated window
lastWnd = curWnd
Else
' we have switched back to our window
Form1.Cls
Form1.Caption = "Activated @ " & Now
Form1.Print "Last Windows Handle = " & lastWnd
Form1.Print "Last Windows Caption: " & GetWindowCaption(lastWnd)
End If
End Select
End Select
WndProc = CallWindowProc(OldProc, hWnd, wMsg, wParam, lParam)
End Function
Public Function GetWindowCaption(ByVal hWnd As Long) As String
Dim r As Long
GetWindowCaption = Space(255)
r = GetWindowText(hWnd, GetWindowCaption, 255)
GetWindowCaption = Left(GetWindowCaption, r)
End Function