Change in rich edit control behaviour with EM_EXSETSEL / SetSel
-
There seems to have been a change in behaviour between VC6 and VS2005 builds when calling SetSel / EM_EXSETSEL in a rich edit control. I have code which emboldens certain lines of text, which used to work in VC6, but since moving to VS2005 has come up with the wrong results. The code is something like:
CString sText;
pEdit->GetWindowText(sText);CString sSection = "SectionHeading";
int nPos = sText.Find(sSection);
int nPosEnd = nPos + sSection.GetLength();
pEdit->SetSel(nPos, nPosEnd);If the heading is at the start of the text, then it's ok, else the selection is offset by the number of lines, which makes we think that the old code counted line breaks as two characters, but the new code counts a line break as one character. In the sText, the line breaks are definately two characters in both VC6 and VS8 builds. Any ideas anyone?
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
-
There seems to have been a change in behaviour between VC6 and VS2005 builds when calling SetSel / EM_EXSETSEL in a rich edit control. I have code which emboldens certain lines of text, which used to work in VC6, but since moving to VS2005 has come up with the wrong results. The code is something like:
CString sText;
pEdit->GetWindowText(sText);CString sSection = "SectionHeading";
int nPos = sText.Find(sSection);
int nPosEnd = nPos + sSection.GetLength();
pEdit->SetSel(nPos, nPosEnd);If the heading is at the start of the text, then it's ok, else the selection is offset by the number of lines, which makes we think that the old code counted line breaks as two characters, but the new code counts a line break as one character. In the sText, the line breaks are definately two characters in both VC6 and VS8 builds. Any ideas anyone?
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
-
There seems to have been a change in behaviour between VC6 and VS2005 builds when calling SetSel / EM_EXSETSEL in a rich edit control. I have code which emboldens certain lines of text, which used to work in VC6, but since moving to VS2005 has come up with the wrong results. The code is something like:
CString sText;
pEdit->GetWindowText(sText);CString sSection = "SectionHeading";
int nPos = sText.Find(sSection);
int nPosEnd = nPos + sSection.GetLength();
pEdit->SetSel(nPos, nPosEnd);If the heading is at the start of the text, then it's ok, else the selection is offset by the number of lines, which makes we think that the old code counted line breaks as two characters, but the new code counts a line break as one character. In the sText, the line breaks are definately two characters in both VC6 and VS8 builds. Any ideas anyone?
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
Have you tried using EM_FINDTEXT to find the text position instead of copying the text to a CString? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
While working with rich edit I found out that setting the selection back to begining is often necessary. Since I work with VC6 I cannot comment on the "rest of the story". Cheers Vaclav
Thanks for your reply. It's not that - I've found it: VC6 used RichEdit 1.0 controls by default, whereas VS8 uses RichEdit 2.0 controls by default, so that is where the difference lies. Version 1.0 seems to count line breaks as 2 chars, whereas 2.0 counts them as 1 char. I was getting my positions from my CString, which contains CR+LFs so counts line breaks as 2 chars. I have changed my code to use the Find facility (EM_FINDTEXT/EM_FINDTEXTEX) in the RichEdit control, and all is now OK. (Not really sure why I didn't just use that in the first place!)
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
-
Thanks for your reply. It's not that - I've found it: VC6 used RichEdit 1.0 controls by default, whereas VS8 uses RichEdit 2.0 controls by default, so that is where the difference lies. Version 1.0 seems to count line breaks as 2 chars, whereas 2.0 counts them as 1 char. I was getting my positions from my CString, which contains CR+LFs so counts line breaks as 2 chars. I have changed my code to use the Find facility (EM_FINDTEXT/EM_FINDTEXTEX) in the RichEdit control, and all is now OK. (Not really sure why I didn't just use that in the first place!)
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
Paul, After I posted my reply I realized that I was responding incorrectly. Anyway, I observed same "problem" - I was searching for string and not counting the space after the string. Consequently my display was off few characters. ( I was changing the text color) By accident - and I am sure you are aware of that - if you search rich text control for string using substring search it will give you the first match and it may not be what you are looking for. Cheers Vaclav
-
Have you tried using EM_FINDTEXT to find the text position instead of copying the text to a CString? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I have now! See my other reply. Thanks.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)