Description & Usage

Sending the EM_GETLINE message to an edit control retrieves the contents of one of its lines. The copied text is placed into the string passed as lParam.

Return Value

If successful, the message returns the number of characters copied into the string passed as lParam. If an error occured (most likely if an invalid line was specified), the message returns zero.

Visual Basic-Specific Issues



The zero-based index of the line to copy into the string passed as lParam.
A string that receives a copy of the contents of the specified line of the edit control. This string must already be long enough to receive the copied text. Also, before sending the message, the first two bytes of the string must contain a 16-bit integer specifying the length of the string. (See the example for a demonstration of how to do this.)

Constant Definitions



Read the first visible line at the top of edit control Text1. Display the text on that line in the Debug window. This requires sending a series of edit control messages, if we do this via the API. To use this example, place a text edit control named Text1 and a command button named Command1 on a form window. Make sure that the MultiLine property of Text1 is set to True before running the example. To get the first line of text, click button Command1.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _
	As Any, ByVal Length As Long)
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal _
	Msg As Long, wParam As Any, lParam As Any) As Long
Public Const EM_GETLINE = &HC4
Public Const EM_LINEINDEX = &HBB
Public Const EM_LINELENGTH = &HC1

' *** Place the following code inside the form window. ***

Private Sub Command1_Click()
	Dim lineindex As Long       ' index of the first visible line
	Dim charindex As Long       ' index of the first character on that line
	Dim linetextlen As Integer  ' length of that line (note the data type!)
	Dim linetext As String      ' receives the line's text
	Dim retval As Long          ' generic return value
	' Get the zero-based index of Text1's first visible line.
	lineindex = SendMessage(Text1.hWnd, EM_GETFIRSTVISIBLELINE, ByVal CLng(0), ByVal CLng(0))
	' Get the zero-based index of the first character on that line.  This is
	' need for the message we'll send next.
	charindex = SendMessage(Text1.hWnd, EM_LINEINDEX, ByVal lineindex, ByVal CLng(0))
	' Find out the number of characters on that line.  Note how
	' we store this in a 16-bit value instead of the regular 32-bit Long.
	linetextlen = SendMessage(Text1.hWnd, EM_LINELENGTH, ByVal charindex, ByVal CLng(0))
	' Make enough room in the string to receive the text.  However,
	' the string must be at least two bytes/characters long for what we'll do next.
	linetext = Space(IIf(linetextlen >= 2, linetextlen, 2))
	' EM_GETLINE wants the length of the string copied into the first
	' two bytes of the string passed to it.  Unusual, but this is the simplest way around
	' the two-message-parameter limit.  This is why linetextlen is only 16 bits long.
	CopyMemory ByVal linetext, linetextlen, Len(linetextlen)
	' Finally, read the first line visible in Text1.
	retval = SendMessage(Text1.hWnd, EM_GETLINE, ByVal lineindex, ByVal linetext)
	' In case we made the string too long (if the line was less than two
	' characters long), shorten it back up.
	If linetextlen < 2 Then linetext = Left(linetext, linetextlen)
	' Finally, display the text.
	Debug.Print "The first line visible in Text1 reads: "; linetext
End Sub

See Also



Edit Controls

Back to the Message list.
Back to the Reference section.

Last Modified: August 26, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
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/e/em_getline.html