GetWindowText Function

Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long

Platforms

Description & Usage

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.

Return Value

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.

Visual Basic-Specific Issues

None.

Parameters

hWnd
A handle to the window to read the title of.
lpString
String variable that receives the window's text. This must already have enough room to receive the string.
nMaxCount
The length in characters of lpString.

Example

' 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

See Also

GetWindowTextLength, SetWindowText, WM_GETTEXT

Category

Windows

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