FindWindowEx Function

Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Platforms

Description & Usage

FindWindowEx searches for a window matching a specified window class name and/or window name. The function searches all of the child windows of a given window (if desired), beginning with the windows below a specified child window. To ignore the window's class and/or title, pass a null string for those parameters.

Return Value

If successful, the function returns a handle to the found window. 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

hWndParentA handle to the parent window to search the child windows of. To search all windows, specify 0 for this parameter.
hWndChildAfterA handle to the child window specifying a place to begin searching. Searching begins with the child window immediately after this window in the Z-order. If this is 0, searching begins with the child window at the top of the Z-order.
lpszClass
The name of the window class of the window to find. Specify a null string to ignore the class.
lpszWindow
The name of the title bar text of the window to find. Specify a null string to ignore the window's title.

Example

Display the name of the first command button on window Form1. The buttons on a form are child windows of the window they appear in. In VB5 and VB6, buttons created in the Form Editor have class "ThunderCommandButton". This search runs when the user clicks button cmdFind, so to use this example, you must first 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 FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, _
	ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" _
	(ByVal hWnd As Long) As Long
Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, _
	ByVal lpString As String, ByVal nMaxCount As Long) As Long

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

Private Sub cmdFind_Click()
	Dim hWnd As Long       ' handle to the found window (the command button)
	Dim slength As Long    ' length of the found window's text
	Dim wintext As String  ' holds the window's text
	Dim retval As Long     ' return value
	
	' Find the "topmost" command button on Form1.  Begin searching at the top.
	hWnd = FindWindowEx(Form1.hWnd, 0, "ThunderCommandButton", vbNullString)
	If hWnd = 0 Then
		Debug.Print "No command buttons of class ThunderCommandButton were found."
	Else
		' Get the text displayed in the button.
		slength = GetWindowTextLength(hWnd)
		wintext = Space(slength + 1)
		retval = GetWindowText(hWnd, wintext, slength + 1)
		' Remove the terminating null and display the result.
		wintext = Left(wintext, slength)
		Debug.Print "The command button's name is: "; wintext
	End If
End Sub

See Also

FindWindow

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/findwindowex.html