CRichEditCtrl 获取单行字符串长度

对于多行RichEdit控件,CRichEditCtrl 提供了获取每行字符长度的函数:LineLength()

定义如下:

Retrieves the length of a line in a rich edit control. int LineLength( int nLine = -1 ) const; Parameters nLine Specifies the character index of a character in the line whose length is to be retrieved. If this parameter is –1, the length of the current line (the line that contains the caret) is returned, not including the length of any selected text within the line. When LineLength is called for a single-line edit control, this parameter is ignored. Return Value When LineLength is called for a multiple-line edit control, the return value is the length (in bytes) of the line specified by nLine. When LineLength is called for a single-line edit control, the return value is the length (in bytes) of the text in the edit control.  

 

在初次使用时,没有完全理解其中nLine的意思,以致出现错误。

nLine -> Specifies the character index of a character in the line whose length is to be retrieved.

指的是 character index 而非 line index,因此,需要找到此行中某一个字符的index,作为参数,获取此行字符长度。

 

每行的第一个字符的character index可以通过函数LineIndex()获得

定义如下:

Retrieves the character index of a line within this CRichEditCtrl object. int LineIndex( int nLine = -1 ) const; Parameters nLine Contains the index value for the desired line in the text of the edit control, or contains –1. If nLine is –1, it specifies the current line, that is, the line that contains the caret. Return Value The character index of the line specified in nLine or –1 if the specified line number is greater then the number of lines in the edit control. 

这里的nLine指的是line index。

 

获取某行字符长度操作如下:

 

int nLineCount = m_richedit.GetLineCount(); int ilineindex=0, nLineLength=0; for (int i=0; i<nLineCount; i++) { ilineindex = m_richedit.LineIndex(i); // character index nLineLength = m_richedit.LineLength(ilineindex); // length } 

 

需要注意的是,在Dialog based的应用程序中使用rich edit control需要show之前调用 AfxInitRichEdit(),

 

If you are using a rich edit control in a dialog box (regardless whether your application is SDI, MDI, or dialog-based), you must call AfxInitRichEdit once before the dialog box is displayed. A typical place to call this function is in your program's InitInstance member function. You do not need to call it for each time you display the dialog box, only the first time. You do not have to call AfxInitRichEdit if you are working with CRichEditView.  

 

 

你可能感兴趣的:(function,application,dialog,character,each)