Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Platforms: Win 32s, Win 95/98, Win NT
GetWindowTextLength returns the length in characters of the text in a window's title bar. You can use this function in conjunction with GetWindowText to create a string just long enough to receive the title. If you do, be sure to make the string 1 character longer than the value the function returns to allow for the ending vbNullChar. The function returns 0 if an error occurs.
Example:
' Display the text of the title bar of window Form1
Dim textlen As Long ' receives length of text of title bar
Dim titlebar As String ' receives the text of the title bar
Dim slength As Long ' receives the length of the returned string
' Find out how many characters are in the window's title bar
textlen = GetWindowTextLength(Form1.hWnd)
titlebar = Space(textlen + 1) ' make room in the buffer, allowing for the terminating null character
slength = GetWindowText(Form1.hWnd, titlebar, textlen + 1) ' read the text of the window
titlebar = Left(titlebar, slength) ' extract information from the buffer
Debug.Print "The title bar of the window: "; titlebar
Related Calls: GetWindowText
Category: Windows
Back to the index.