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
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.
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.
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.
' 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
Last Modified: July 31, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information