Solved 2003 **Hooks to get last application with focus

Posted on 2006-04-06
Last Modified: 2012-06-21
I have found a few posts by IDLE MIND and IMRAN.  Idle Mind had a good code which works with VB6. Unable to find now.  He used Shell scripts to complete.  

Intercepting WM_ACTIVATE message for any window (detecting activation with hooks/subclassing)

uMsgNotify = RegisterWindowMessage("SHELLHOOK")

    ' This basically registers our window to receive the shell events
Call RegisterShellHookWindow = (Me.Handle)
UMsgNotify has an error that it expects a declaration
Call states Syntax error.

I have another open question just as this one.. Just looking for a solution.
Help.  Help Help!  
Question by:TechMonster
    1 Comment
    LVL 85

    Accepted Solution

    Like so...

    Imports System.Runtime.InteropServices

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Public Enum ShellEvents
            HSHELL_GETMINRECT = 5
            HSHELL_REDRAW = 6
            HSHELL_TASKMAN = 7
            HSHELL_LANGUAGE = 8
        End Enum

        Public Declare Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer
        Public Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
        Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
        Public Declare Function GetForegroundWindow Lib "user32" () As IntPtr
        Public Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As IntPtr) As Integer

        Private uMsgNotify As Integer
        Private lastWindow As IntPtr

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            uMsgNotify = RegisterWindowMessage("SHELLHOOK")
        End Sub

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = uMsgNotify Then
                Select Case m.WParam.ToInt32
                    Case ShellEvents.HSHELL_WINDOWACTIVATED
                        Debug.WriteLine("window activated")
                        Dim curWindow As IntPtr = GetForegroundWindow()
                        If (Not curWindow.Equals(IntPtr.Zero)) AndAlso (Not curWindow.Equals(Me.Handle)) Then
                            lastWindow = curWindow
                        End If
                End Select
            End If
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not lastWindow.Equals(IntPtr.Zero) Then
                SendKeys.Send("Hello World...")
            End If
        End Sub

        Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        End Sub

    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now