Do not use on any
shared computer
August 30, 2008 01:14am pdt
 
[x]
Attachment Details

SHELLHOOK, RegisterWindowMessage(), RegisterShellHookWindow(), HSHELL_WINDOWACTIVATED, WM_ACTIVATE, Last Active / Focus Window

Tags: registerwindowmessage
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.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/ShellProc.asp


This 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("SHELLHOOK")
    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_WINDOWACTIVATED
                    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
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: Idle_Mind
Solution Provided By: EDDYKT
Participating Experts: 4
Solution Grade: A
Views: 26
Translate:
Loading Advertisement...
 
[+][-]Accepted Solution by EDDYKT

Rank: Genius

Accepted Solution by EDDYKT:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by Idle_Mind
Author Comment by Idle_Mind:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by egl1044

Rank: Sage

Assisted Solution by egl1044:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by Idle_Mind
Author Comment by Idle_Mind:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by egl1044

Rank: Sage

Assisted Solution by egl1044:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by Ark

Rank: Sage

Assisted Solution by Ark:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by Idle_Mind
Author Comment by Idle_Mind:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Administrative Comment by DanRollins
Administrative Comment by DanRollins:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by ttobin333
There is an incomplete "Call" statement in the form load subroutine. What is the rest of that line supposed to be?

Thanks.
 
 
Comment by Idle_Mind
Hi ttobin333,

It's all there...what error are you getting?
 
 
20080723-EE-VQP-34