# GetActiveWindow Function

`Declare Function GetActiveWindow Lib "user32.dll" () As Long`

GetActiveWindow returns a handle to your program's currently active window. This only works with windows created by your application -- in other words, it won't find the active window of other programs. If your program is in the background, the function will get the window that would be active. If an error occurs, or if there is no active window to your program, the function instead returns 0.

Example:

    ' Flash the program's active window once
    active = GetActiveWindow()  ' get the handle of the program's active window
    If active = 0 Then Exit Sub  ' abort if there is no active window
    ' The next three lines flash this window using FlashWindow and Sleep
    x = FlashWindow(active, 1): Sleep 250
    x = FlashWindow(active, 1): Sleep 250
    x = FlashWindow(active, 0)

Related Call: GetWindow
Category: Windows
Back to the index.