GetForegroundWindow Function

Declare Function GetForegroundWindow Lib "user32.dll" () As Long

Platforms: Win 95/98, Win NT

GetForegroundWindow finds which window is currently the foreground window. The foreground window is the window, usually at the top of the Z-order, with which the user is currently working with -- i.e., the window with the focus. The function returns 0 if an error occured, or the handle of the foreground window if successful.

Example:

' Display the title bar text of the foreground window.
Dim hforewnd As Long  ' receives handle of foreground window
Dim slength As Long  ' length of foreground window's title bar text
Dim wintext As String  ' buffer for foreground window's title bar text
Dim retval As Long  ' return value

hforewnd = GetForegroundWindow()  ' determine the foreground window
slength = GetWindowTextLength(hforewnd) + 1  ' length of its title bar text
wintext = Space(slength)  ' make room in the buffer to receive the text
retval = GetWindowText(hforewnd, wintext, slength)  ' get title bar text
wintext = Left(wintext, slength - 1)  ' remove null character from end of string
Debug.Print "The window "; wintext; " is the foreground window."

See Also: SetForegroundWindow
Category: Windows

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getforegroundwindow.html