Chapter 27: Using the Windows Application Programming Interface
961
The window’s handle is passed as an argument, and the function places the window’s class name
in the string lpClassName. The function returns the number of characters copied to the string
buffer if it’s successful, 0 if not.
The DisplayTitle wrapper function (see Listing 27.5) uses GetParent and GetClassNameA
to cycle through the windows and controls of an open Access form until the Access application
form (class Omain) is located. It then captures the text displayed in the title bar, using
GetWindowTextA, and displays the text in a message box.
LISTING 27.5
Returning the Caption Bar Text for a Window
Sub DisplayTitle()
Dim RetVal As Long
Dim Parent As Long
Dim lngRet As Long
Dim Caption As String * 128
Dim CaptionSize As Long
Dim WinHwnd As Long
Dim Class As String * 6
Dim ClassSize As Long
CaptionSize = Len(Caption)
ClassSize = Len(Class)
WinHwnd = Me.hwnd
Do Until Trim(Class) = “OMain”
Parent = apiGetParent(WinHwnd)
lngRet = apiGetClassName(Parent, Class, ClassSize)
WinHwnd = Parent
Loop
RetVal = apiGetWindowText( _
Parent, Caption, CaptionSize)
MsgBox Left$(Caption, RetVal)
End Sub
Notice that the Do...Loop in the middle of DisplayTitle searches for a window with a class
name of OMain. The very first version of Microsoft’s desktop database project was named Omega,
and this very early precursor to Microsoft Access lives on as the class name applied to the Access
main window.
SetWindowTextA
Use SetWindowTextA to change the caption displayed in a window’s title bar.
Declare Function apiSetWindowText _
Lib “User32” _