Determining the Active Window

This small project demonstrates how to find the active window on the windows desktop. It uses a timer control to fire events every 10th of a second to use the windows GetForeGround window API call to determine the window, and then the GetWindowText api call to get that windows caption.

In the example project, all window captions are printed to the debug window, although you could quite easily watch for any window being activated and then monitored in the background using this method.


'
' In a form, with a Timer control (timer1)
'
private Declare Function GetForegroundWindow Lib "user32" () _
        as Long
private Declare Function GetWindowText Lib "user32" _
    Alias "GetWindowTextA" (byval hwnd as Long, _
       byval lpString as string, byval cch as Long) as Long
'
private Sub Form_Load()
    Timer1.Interval = 100
End Sub
'
private Sub Timer1_Timer()
    static lHwnd as Long
    Dim lCurHwnd as Long
    Dim sText as string * 255
'
    lCurHwnd = GetForegroundWindow
    If lCurHwnd = lHwnd then Exit Sub
    lHwnd = lCurHwnd
    If lHwnd <> hwnd then
        Caption = "ActiveWidow: " & Left$(sText, _
            GetWindowText(lHwnd, byval sText, 255))
    else
        Caption = "ActiveWindow: Form1"
    End If
End Sub
'
'

Download zipped project (3k)


Comments

  • A little alteration

    Posted by david on 11/28/2012 02:07pm

    could this be used with something like a sendkeys program to automate sending keystrokes to a program. And will some post an example.

    Reply
  • How to know the EXE filename linked to the active windows?

    Posted by rei_vilo on 09/26/2004 05:54am

    Great code to know the active window, it works fine... but how to get the EXE filename of the app of this active windows ? Thanks!

    Reply
  • How do I determine for specific application whether it is runnign or close?

    Posted by Legacy on 11/05/2003 12:00am

    Originally posted by: Mamta Shah

    Please help me in order to determine if I looking for specific vb application that is running or Closed.

    Thanks,
    Mamta

    Reply
  • x

    Posted by Legacy on 06/24/2002 12:00am

    Originally posted by: kishore

    i want to capture , the form, event name when ever it gives error message while running vb project

    with regards
    kishore

    Reply
  • Very useful

    Posted by Legacy on 04/27/2002 12:00am

    Originally posted by: Serdar GERE

    That's very useful and simple code. thanks.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds