Call this function to retrieve the line number of the line that contains the specified character index.

int LineFromChar( 
   int nIndex = -1  
) const;


Contains the zero-based index value for the desired character in the text of the edit control, or contains –1. If nIndex is –1, it specifies the current line, that is, the line that contains the caret.

The zero-based line number of the line containing the character index specified by nIndex. If nIndex is –1, the number of the line that contains the first character of the selection is returned. If there is no selection, the current line number is returned.

A character index is the number of characters from the beginning of the edit control.

This member function is only used by multiple-line edit controls.

For more information, see EM_LINEFROMCHAR in the Windows SDK.

// The index of the char to get information on. 
int nIndex = 4;
CString strText;

strText = strText.Mid(nIndex, 1);

// Get the text extent of the character.
CDC* pDC = m_myEdit.GetDC();
CSize sz = pDC->GetTextExtent(strText);

CPoint pt = m_myEdit.PosFromChar(nIndex);

// Dump the index, character, line number, and character bounds.
TRACE(_T("nIndex = %d, character = %c, line = %d, bounds = ")
   _T("{%d, %d, %d, %d}\r\n"),
   nIndex, strText[0], m_myEdit.LineFromChar(nIndex),
   pt.x /* left */, pt.y /* top */,
   pt.x+sz.cx /* right */, pt.y+sz.cy /* bottom */);

Header: afxwin.h

Community Additions