# GetTopWindow Function

`Declare Function GetTopWindow Lib "user32.dll" (ByVal hwnd As Long) As Long`

GetTopWindow returns a handle to the currently active child window of a window. The active child window is the one that has the focus, and it is usually at the top of all the other children in the Z-order. This function works even if the parent window is not active. If an error occurs or the window has no children, the function instead returns 0. This function is identical to calling GetWindow using the GW_CHILD relationship.

hwnd
  The handle of the parent window. The function will return its active child window.

Example:

    ' Flash MDI form MDIForm1's active child
    active = GetTopWindow(MDIForm1.hWnd)  ' get the handle of MDIForm1's active child
    If active = 0 Then Exit Sub  ' abort if there is no child 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.