FindWindowEx Function

Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As Any, ByVal lpszWindow As Any) 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. If you do not wish to specify a window class name or a window name, you must pass 0 for those parameters -- merely passing an empty string does not work.

Return Value

If an error occured or no matching windows could be found, the function returns 0 (use GetLastError to get the error code). If the function finds the window, the function returns a handle to that window.

Visual Basic-Specific Issues

When explicitly passing a 0 as lpszClass or lpszWindow, 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

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. Pass 0 to allow the window to be of any class.
lpszWindow
The name of the title bar text of the window to find. Pass 0 to allow the window to have any name.

Example

' Display the name of the command button at the top of the Z-order
' (which doesn't make much sense in this context) on window Form1.  The command
' button is considered a child window of Form1.  In Visual Basic 5, the class name
' of command buttons is "ThunderCommandButton".  Remember, command buttons
' are just another type of window!
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.
' (Note how the CLng function must be used to force 0 to be a Long.)
hwnd = FindWindowEx(Form1.hWnd, 0, "ThunderCommandButton", CLng(0))
If hwnd = 0 Then  ' if no such windows exist
  Debug.Print "No command buttons of class ThunderCommandButton were found."
Else
  ' Display the name (text) of the found window.
  slength = GetWindowTextLength(hwnd)  ' get length of text
  wintext = Space(slength + 1)  ' make enough room in the buffer, including the trailing null
  retval = GetWindowText(hwnd, wintext, slength + 1)  ' get text
  wintext = Left(wintext, slength)  ' remove the trailing null character
  Debug.Print "The command button's name is: "; wintext  ' display the result
End If

Related Function

FindWindow

Category

Windows

Back to the index.


Last Modified: July 31, 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/findwindowex.html