RichEdit has a strange behavior. The WM_GETTEXTLENGTH
message considers the new line characters as two characters \r\n
, while the EM_SETSEL
and EM_EXSETSEL
messages consider them as one character \n
.
This creates confusion and lot of bad things, for example the below function does not work well:
void CutLastFiveChars(HWND hRichEdit) {
// Get the current text length
int textLength = (int)SendMessage(hRichEdit, WM_GETTEXTLENGTH, 0, 0);
// Calculate the starting position for the selection
int startPos = textLength - 5;
if (startPos < 0) {
startPos = 0; // Ensure startPos is within bounds
}
// Set the selection to the last 5 characters
SendMessage(hRichEdit, EM_SETSEL, startPos, textLength);
// Replace the selected text with an empty string
SendMessage(hRichEdit, EM_REPLACESEL, TRUE, (LPARAM)"");
}
How can I fix this?
Or, is there a way to disable automatically converting \n
characters into \r\n
?