FindWindow Function

Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long

Platforms

Description & Usage

FindWindow searches all windows for one which matches the window class name and/or window name. The function's searching mechanism is not case-sensitive. If you do not wish to specify either a class name or window name, you must pass a 0 for that parameter -- passing merely an empty string does not work.

Return Value

If an error occured or a matching window could not be found, the function returns 0 (use GetLastError to get the error code). If successful, the function returns a handle to the window which it found.

Visual Basic-Specific Issues

When explicitly passing a 0 as either parameter, you must use the CLng function on it to explicitly make the parameter a Long. Visual Basic does not allow ordinary numeric literals to be passed as Any data type.

Parameters

lpClassName
The name of the window class of the window to find. Pass 0 to allow the window to be of any class.
lpWindowName
The name of the title bar text of the window to find. Pass 0 to allow the window to have any name.

Example

' Search for a window called Minesweeper and flash its title bar
' once.  We don't need to know the name of that window's class in order to
' find it, but we could include it if we wanted.
Dim hwnd As Long  ' receives handle to the found window
Dim retval As Long  ' generic return value

' Attempt to locate a window called Minesweeper.  Note how the CLng function
' must be used to force 0 as a Long data type.
hwnd = FindWindow(CLng(0), "Minesweeper")  ' look for the window
If hwnd = 0 Then  ' could not find the window
  Debug.Print "Minesweeper is not currently running."  ' if it were, there'd be a window!
Else
  ' Flash the window's title bar on and off once.
  retval = FlashWindow(hwnd, 1)
  Sleep 500  ' pause for half a second
  retval = FlashWindow(hwnd, 0)
End If

Related Function

FindWindowEx

Category

Windows

Back to the index.


Last Modified: October 11, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information

Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/f/findwindow.html