Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to set text color in CRichEditCtrl?

How to set text color in CRichEditCtrl?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David YueZuo
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • D David YueZuo

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Should this line m_richEditCtrl.SetSel(length, newLength ); be m_richEditCtrl.SetSel(length, newLength - 1 ); ??

      D 1 Reply Last reply
      0
      • M Mark Salsbery

        Should this line m_richEditCtrl.SetSel(length, newLength ); be m_richEditCtrl.SetSel(length, newLength - 1 ); ??

        D Offline
        D Offline
        David YueZuo
        wrote on last edited by
        #3

        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

        M D 2 Replies Last reply
        0
        • D David YueZuo

          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

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • D David YueZuo

            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

            D Offline
            D Offline
            David YueZuo
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups