Votes in Release
VB Code which identifies who's stealing focus


This is some code I wrote to figure out what process was stealing the focus every 15 minutes, which was ruining my RPG playing. (It turns out it was a "call home" updater for some printer software I'd installed, and this helped me identify the culprit and eliminate it.) I discuss the code in some detail at http://blogs.msdn.com/vbteam/archive/2007/04/02/it-s-elementary-using-vb-to-get-process-information-matt-gertz.aspx.

Declare Auto Function GetForegroundWindow Lib "user32.dll" () As Integer
Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Integer, ByRef procid As Integer) As UInteger
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.RichTextBox1.AppendText("Starting up at " & Now & vbCrLf)
    End Sub
    Private Sub GoingAway(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate, Me.LostFocus
        Dim hwnd As Integer = GetForegroundWindow()
        ' Note that process_id will be used as a ByRef argument
        ' and will be changed by GetWindowThreadProcessId
        Dim process_id As Integer = 1
        GetWindowThreadProcessId(hwnd, process_id)
        If (process_id <> 1) Then
            Dim appExePath As String = Process.GetProcessById(process_id).MainModule.FileName() 
            Me.RichTextBox1.AppendText("Lost focus at " & Now & " due to " & appExePath & vbCrLf)
            Me.RichTextBox1.AppendText("Lost focus due to unknown cause.")
        End If
    End Sub


There are no files associated with this release.

Page view tracker