Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
GetWindowText retrieves the text of a window. For regular windows, this is the text which appears in the title bar. For controls, this is the text in the control. Note that GetWindowText cannot retrieve the text in a control owned by another program. To get that text, use the WM_GETTEXT message instead.
If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns the number of characters copied into the string passed as lpString, not counting the terminating null character.
None.
' This code is licensed according to the terms and conditions listed here.
' Display the text displayed in the title bar of window Form1
Dim textlen As Long ' receives length of text of the window
Dim wintext As String ' receives the text of the window
Dim slength As Long ' receives the length of the returned string
' Find out how many characters are in the window's text.
' Add 1 to compensate for the terminating null.
textlen = GetWindowTextLength(Form1.hWnd) + 1
' Make sufficient room in the buffer.
wintext = Space(textlen)
' Retrieve the text of window Form1.
slength = GetWindowText(Form1.hWnd, wintext, textlen)
' Remove the empty space from the string, if any.
wintext = Left(wintext, slength)
' Display the result.
Debug.Print "The title bar of window Form1 is: "; wintext
GetWindowTextLength, SetWindowText, WM_GETTEXT
Go back to the alphabetical Function listing.
Go back to the Reference section index.
Last Modified: February 11, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
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/g/getwindowtext.html