EM_LINELENGTH Message

Platforms

Description & Usage

Sending the EM_LINELENGTH message to an edit control find out how many characters are on a particular line. This message find out the length of one of the control's lines, taking word wrapping into account. However, for some bizarre reason, the line is idenified by a character position and not a line index value.

Return Value

For multi-line edit controls, the message returns the number of characters on the specified line. For single-line edit controls, the message returns the total number of characters in the control.

Visual Basic-Specific Issues

None.

Parameters

wParam
The zero-based index of any character on the line to get the length of. Note that this is a character index, not a line index. (To get the index of the first character on a particular line, send the EM_LINEINDEX message before sending this message.) If this parameter is -1, the function returns the total number of unselected characters that are on lines that contain selected characters.
lParam
Not used -- set to 0.

Constant Definitions

Const EM_LINELENGTH = &HC1

Example

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_GETFIRSTVISIBLELINE = &HCE
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

EM_GETLINE

Category

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_linelength.html