CRichEditCtrl::LineLength returns strange result
Marty Wagner -- marty@concentra.com
Wednesday, March 27, 1996
Platform:
NT 3.1, 48 megs RAM, VC++ 4.0
I tried to use CRichEditCtrl::LineLength to get the length of a line
and found that it didn't return what I thought it would return.
Often, it
would return a number that was clearly less than the actual number of
characters in the line. I know that LineLength is supposed to ignore
selected characters, but in this case, there are no characters that
are selected.
I have a simple test to show this. It requires adding an OnChar
handler for CWordPadView in the WordPad sample application. Whever
the user enters
a carriage return, it looks at the previous line and calls LineLength
on that line.
Then it calls GetLine and prints out the line that it gets, along
with the length
of the line according to GetLine.
If I run WordPad and type the following (where means carriage
return. Note the inclusionn
of spaces!):
1 2 3
I get the following printed out. Note that the actual size is correct
(takes carriage return/line
feedd into account). However, the length as reported by LineLength is
alway 1 even though I keep
adding spaces)
wordpad: The length of line 0 is 1 chars
wordpad: The string found was "1
"
Its actual size is 3
wordpad: The length of line 1 is 1 chars
wordpad: The string found was " 2
"
Its actual size is 4
wordpad: The length of line 2 is 1 chars
wordpad: The string found was " 3
"
Its actual size is 5
void CWordPadView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CRichEditView::OnChar(nChar, nRepCnt, nFlags);
if (VK_RETURN == nChar)
{
long nCurLine = GetRichEditCtrl().LineFromChar (-1);
if (0 == nCurLine)
return;
long nPrevLine = nCurLine - 1;
int nLineLength = GetRichEditCtrl().LineLength
(nPrevLine);
TRACE ("The length of line %d is %d chars\n",
nPrevLine, nLineLength);
char* ptsLine = new char [128];
int nActualLine = GetRichEditCtrl().GetLine
(nPrevLine, ptsLine, 128);
CString sStr (ptsLine, nActualLine);
TRACE ("The string found was \"%s\"\n", sStr);
}
}
Marty Wagner
Concentra Corporation
marty@concentra.com
Oleg Moroz -- moroz@inist.ru
Saturday, March 30, 1996
Note that an argument to LineLength method (both in CRichEditCtrl and
CEdit) is not a line index (although some clever man at MS called it
nLine), it's _character_ index... I've stumbled upon this too...
Oleg
| Вернуться в корень Архива
|