Menu

#200 Ability to change underline color in TRichEdit

8
pending
GUI (33)
1
2026-02-26
2022-03-05
No

The TRichEdit control supports the functionality to not only change the underline style, but its color, via the TCharFormat structure.

The colors that can be used are listed in the documentation as these:

UnderlineColor_Black = 0x00;
UnderlineColor_Blue = 0x01;
UnderlineColor_Aqua = 0x02;
UnderlineColor_Lime = 0x03;
UnderlineColor_Fuchsia = 0x04;
UnderlineColor_Red = 0x05;
UnderlineColor_Yellow = 0x06;
UnderlineColor_White = 0x07;
UnderlineColor_Navy = 0x08;
UnderlineColor_Teal = 0x09;
UnderlineColor_Green = 0x0A;
UnderlineColor_Purple = 0x0B;
UnderlineColor_Maroon = 0x0C;
UnderlineColor_Olive = 0x0D;
UnderlineColor_DkGray = 0x0E;
UnderlineColor_LtGray = 0x0F;

But those constants are not defined in the Windows SDK headers, and also have to be set in a different way for RichEdit 2.0 and for RichEdit 4.1.

For RichEdit 2.0: Shift the color code of the above constants one digit to the left, and OR it with the underline style.

For RichEdit 4.1: Add 1 to the color code, and set it in the bUnderlineColor member.

Related

Wiki: OWLNext_Roadmap_and_Prereleases

Discussion

  • Vidar Hasfjord

    Vidar Hasfjord - 2022-03-05
    • labels: --> GUI
    • summary: Add abilityto change underline color in TRichEdit --> Add ability to change underline color in TRichEdit
     
  • Vidar Hasfjord

    Vidar Hasfjord - 2022-03-05
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,7 +1,8 @@
    -The TRichEdit control supports the functionality to not only change the underline style, but it's color, via the TCharFormat structure.
    +The TRichEdit control supports the functionality to not only change the underline style, but its color, via the TCharFormat structure.
    
     The colors that can be used are listed in the [documentation](https://docs.microsoft.com/en-us/windows/win32/api/richedit/ns-richedit-charformat2a) as these:
    -~~~
    +
    +~~~C++
     UnderlineColor_Black = 0x00;
     UnderlineColor_Blue = 0x01;
     UnderlineColor_Aqua = 0x02;
    
     
  • Vidar Hasfjord

    Vidar Hasfjord - 2022-03-08

    Hi Jogy,

    I have reviewed [r5868], which added this feature on the trunk. Here are some suggestions:

    • Use enum class for enum TUnderlineColor, with shorter names (Black, etc.).
    • Create an enum class for underline types (styles).
    • SetUnderlineType and SetUnderlineColor should call EnableUnderline for convenience.
    • Replace SetUnderlineType and SetUnderlineColor by an overload set for a new function SetUnderline, having one or both of enum types TUnderlineType and TUnderlineColor as parameters. These functions should also call EnableUnderline.
    • Also, for convenience and nonsurprising behaviour, I propose that SetUnderline should set the mask (CFM_UNDERLINETYPE) to indicate the fields bUnderlineType and bUnderlineColor are valid. If not set, the type should be set to solid by default (CFU_UNDERLINE), otherwise no underline is shown for version 2 (CFU_UNDERLINENONE will be used as default, which disables underline for version 2, but has the same effect as CFU_UNDERLINE for version 4).

    To illustrate the difference between Rich Edit 2 and 4, as regards default underline type:

    using C = TCharFormat::TUnderlineColor;
    auto blueUnderline = TCharFormat{}.EnableUnderline();
    if (TRichEditDll::Dll()->GetVersion() >= 4)
    {
      // Note that we still get a solid line (same as CFU_UNDERLINE), even though we use CFU_UNDERLINENONE (default).
      //
      blueUnderline.bUnderlineType = CFU_UNDERLINENONE; // 0 (default)
      blueUnderline.bUnderlineColor = static_cast<uint8>(C::Blue) + 1;
    }
    else
    {
      // Note that CFU_UNDERLINE is needed, as CFU_UNDERLINENONE (default) disables the underline.
      //
      blueUnderline.bUnderlineType = (static_cast<uint8>(C::Blue) << 4) | CFU_UNDERLINE;
    }
    blueUnderline.dwMask |= CFM_UNDERLINETYPE; // Enable our type and color settings.
    

    Note: The use of enum class requires C++11 and can hence not be used on the 6.44 branch. However, classic enums can be used there, with just a slight API difference as regards qualification (e.g. TCharFormat::TUnderlineColor::Black vs TCharFormat::Black).

    I have implemented the suggested improvements on the trunk in [r5869]. The revision also includes an update and a few changes to the test code in "examples/classes".

     
    👍
    1

    Related

    Commit: [r5868]
    Commit: [r5869]

  • Vidar Hasfjord

    Vidar Hasfjord - 2022-03-09

    Support for mapping from TColor to TUnderlineColor in TCharFormat, with new overloads SetUnderline(TColor) and SetUnderline(TUnderlineType, TColor), was added in [r5870] (with an internal improvement to the mapping algorithm in [r5872]).

     
    👍
    1

    Related

    Commit: [r5870]
    Commit: [r5872]

  • Vidar Hasfjord

    Vidar Hasfjord - 2022-06-18
    • summary: Add ability to change underline color in TRichEdit --> Ability to change underline color in TRichEdit
    • Group: 6.44 --> 7.1
     
  • Ognyan Chernokozhev

    • status: open --> pending
     

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB