FindWindow Function

Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 one of the parameters, pass a null string for it.

Return Value

If successful, the function returns a handle to the window that was found. If no matching window could be found, or if an error occured, the function returns zero (use GetLastError to get the error code).

Visual Basic-Specific Issues

To pass a null string as one of the function's parameters, use the vbNullString constant.

Parameters

lpClassName
The name of the window class of the window to find. To ignore the window's class, specify a null string.
lpWindowName
The name of the title bar text of the window to find. To ignore the window's text, specify a null string.

Example

Search for a window called Minesweeper and flash its title bar once. We don't need to know the name of the window's class to find it, since most likely there won't be any unless Windows's Minesweeper game is running. This is all done when the user clicks button cmdFind, so to use this example, you naturally must place a command button named cmdFind on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
	ByVal lpWindowName As String) As Long
Public Declare Function FlashWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal bInvert As Long) As Long
Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

' *** Place the following code inside a form. ***

Private Sub cmdFind_Click()
	Dim hWnd As Long    ' receives handle to the found window
	Dim retval As Long  ' generic return value
	
	' Attempt to locate a window titled Minesweeper.
	hWnd = FindWindow(vbNullString, "Minesweeper")
	If hWnd = 0 Then
		Debug.Print "Minesweeper is not currently running."
	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
End Sub

See Also

FindWindowEx

Category

Windows

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


Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
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/f/findwindow.html