How to set text color in CRichEditCtrl?
-
Hi all, Who can help me for my below question? I will appriciate that. In my MFC project, I used RichEdit replacing RichEdit2, but the color of the text are shifted one unit. My code: CString strings[3] = {_T("Apple"), _T("Orange"), _T("Pear")}; for (int i = 0; i < 3; i ++) { CHARFORMAT2 cf; m_richEditCtrl.GetSelectionCharFormat(cf); if ( i % 2 == 0 ) { cf.crTextColor = (DWORD) RGB(255, 0 , 0); // Red color } else { cf.crTextColor = (DWORD) RGB(0, 0, 225); // Blue color } cf.dwEffects = static_cast(~CFE_AUTOCOLOR); cf.dwMask = CFM_COLOR; long length = m_richEditCtrl.GetTextLength(); m_richEditCtrl.SetSel(length, -1); m_richEditCtrl.ReplaceSel(strings[i]); long newLength = m_richEditCtrl.GetTextLength(); m_richEditCtrl.SetSel(length, newLength ); m_richEditCtrl.SetSelectionCharFormat(cf); } When I used RichEdit1 the program works perfect, but after I used RichEdit2 the color of text shows weird. Accordding to the code, "Apple" should show red color, "Orange" should show blue color, "Pear" should show red color again. However, the actual text color are: letters "Apple" are red, "O" is red too, "range pe" are blue , and "ar" are red. That doesn't make any sense....:confused: Is there anyone can help me on that? :) Thanks!
David Zuo
-
Hi all, Who can help me for my below question? I will appriciate that. In my MFC project, I used RichEdit replacing RichEdit2, but the color of the text are shifted one unit. My code: CString strings[3] = {_T("Apple"), _T("Orange"), _T("Pear")}; for (int i = 0; i < 3; i ++) { CHARFORMAT2 cf; m_richEditCtrl.GetSelectionCharFormat(cf); if ( i % 2 == 0 ) { cf.crTextColor = (DWORD) RGB(255, 0 , 0); // Red color } else { cf.crTextColor = (DWORD) RGB(0, 0, 225); // Blue color } cf.dwEffects = static_cast(~CFE_AUTOCOLOR); cf.dwMask = CFM_COLOR; long length = m_richEditCtrl.GetTextLength(); m_richEditCtrl.SetSel(length, -1); m_richEditCtrl.ReplaceSel(strings[i]); long newLength = m_richEditCtrl.GetTextLength(); m_richEditCtrl.SetSel(length, newLength ); m_richEditCtrl.SetSelectionCharFormat(cf); } When I used RichEdit1 the program works perfect, but after I used RichEdit2 the color of text shows weird. Accordding to the code, "Apple" should show red color, "Orange" should show blue color, "Pear" should show red color again. However, the actual text color are: letters "Apple" are red, "O" is red too, "range pe" are blue , and "ar" are red. That doesn't make any sense....:confused: Is there anyone can help me on that? :) Thanks!
David Zuo
Should this line m_richEditCtrl.SetSel(length, newLength ); be m_richEditCtrl.SetSel(length, newLength - 1 ); ??
-
Should this line m_richEditCtrl.SetSel(length, newLength ); be m_richEditCtrl.SetSel(length, newLength - 1 ); ??
I tried m_richEditCtrl.SetSel(length, newLength - 1 ), but it still doesn't work well. Actually, what I found is RichEdit2 considers '\n' as two symbols - "\r\n", so if we use m_richEditCtrl.SetWindowText("12345\n"); int length = richEditCtrl.GetWindowText(); // the actual value of length equals to 7, not 6. Again, if we have m_richEditCtrl.SetWindowText("12345\n67890"); int length = richEditCtrl.GetWindowText(); // equals to 12, not 11. richEditCtrl.SetSel(0, 6); m_richEditCtrl.SetSelColor(Red); // pseudo-code m_richEditCtrl.SetSel(7, -1); // This function doesn't set correct text m_richEditCtrl.SetSelColor(Blue); // pseudo-code /// The actual text color is, "123456" is red, "7890" is blue /// If you put more letters and '\n' into the string, you will find the text color shifts more than you think but, if we use m_richEditCtrl.SetWindowText("12345\n67890"); int length = richEditCtrl.GetWindowText(); // equals to 12, not 11. richEditCtrl.SetSel(0, 5); m_richEditCtrl.SetSelColor(Red); // pseudo-code m_richEditCtrl.SetSel(6, -1); m_richEditCtrl.SetSelColor(Blue); // pseudo-code /// program now works well What I found is, the number of the selected text SetSel() function shifts that equals to the number of '\n' being used in the string. Is it weird? Most time I cann't understand why Microsoft still recommand us to use Visual C++, MFC is too old to use. They always use very weird way to do some crazy things. Doesn't make any sense! Furthermore, so manys bugs are found in MFC library, it seems Microsoft never tested their APIs before they released them.
David Zuo
-
I tried m_richEditCtrl.SetSel(length, newLength - 1 ), but it still doesn't work well. Actually, what I found is RichEdit2 considers '\n' as two symbols - "\r\n", so if we use m_richEditCtrl.SetWindowText("12345\n"); int length = richEditCtrl.GetWindowText(); // the actual value of length equals to 7, not 6. Again, if we have m_richEditCtrl.SetWindowText("12345\n67890"); int length = richEditCtrl.GetWindowText(); // equals to 12, not 11. richEditCtrl.SetSel(0, 6); m_richEditCtrl.SetSelColor(Red); // pseudo-code m_richEditCtrl.SetSel(7, -1); // This function doesn't set correct text m_richEditCtrl.SetSelColor(Blue); // pseudo-code /// The actual text color is, "123456" is red, "7890" is blue /// If you put more letters and '\n' into the string, you will find the text color shifts more than you think but, if we use m_richEditCtrl.SetWindowText("12345\n67890"); int length = richEditCtrl.GetWindowText(); // equals to 12, not 11. richEditCtrl.SetSel(0, 5); m_richEditCtrl.SetSelColor(Red); // pseudo-code m_richEditCtrl.SetSel(6, -1); m_richEditCtrl.SetSelColor(Blue); // pseudo-code /// program now works well What I found is, the number of the selected text SetSel() function shifts that equals to the number of '\n' being used in the string. Is it weird? Most time I cann't understand why Microsoft still recommand us to use Visual C++, MFC is too old to use. They always use very weird way to do some crazy things. Doesn't make any sense! Furthermore, so manys bugs are found in MFC library, it seems Microsoft never tested their APIs before they released them.
David Zuo
Glad you got it working. I never saw any newlines (\n) in your original code :)
David.YueZuo wrote:
Furthermore, so manys bugs are found in MFC library, it seems Microsoft never tested their APIs before they released them.
Wow what MFC version are you using? MFC works as documented for me (Version 7.11).
David.YueZuo wrote:
MFC is too old to use
So use .NET! Better yet, use Windows APIs directly! I didn't know Microsoft "recommand us to use Visual C++" either. This is all news to me, thanks! :omg: Mark
-
I tried m_richEditCtrl.SetSel(length, newLength - 1 ), but it still doesn't work well. Actually, what I found is RichEdit2 considers '\n' as two symbols - "\r\n", so if we use m_richEditCtrl.SetWindowText("12345\n"); int length = richEditCtrl.GetWindowText(); // the actual value of length equals to 7, not 6. Again, if we have m_richEditCtrl.SetWindowText("12345\n67890"); int length = richEditCtrl.GetWindowText(); // equals to 12, not 11. richEditCtrl.SetSel(0, 6); m_richEditCtrl.SetSelColor(Red); // pseudo-code m_richEditCtrl.SetSel(7, -1); // This function doesn't set correct text m_richEditCtrl.SetSelColor(Blue); // pseudo-code /// The actual text color is, "123456" is red, "7890" is blue /// If you put more letters and '\n' into the string, you will find the text color shifts more than you think but, if we use m_richEditCtrl.SetWindowText("12345\n67890"); int length = richEditCtrl.GetWindowText(); // equals to 12, not 11. richEditCtrl.SetSel(0, 5); m_richEditCtrl.SetSelColor(Red); // pseudo-code m_richEditCtrl.SetSel(6, -1); m_richEditCtrl.SetSelColor(Blue); // pseudo-code /// program now works well What I found is, the number of the selected text SetSel() function shifts that equals to the number of '\n' being used in the string. Is it weird? Most time I cann't understand why Microsoft still recommand us to use Visual C++, MFC is too old to use. They always use very weird way to do some crazy things. Doesn't make any sense! Furthermore, so manys bugs are found in MFC library, it seems Microsoft never tested their APIs before they released them.
David Zuo
LOL..thx for your suggestions, i will keep that in my mind. Unfortunately, my team have to use VC++ since they started using it at 1998. Maybe I should get my job changed, how about become to a java developer, would be better? hahaha...:)
David Zuo