Search code examples
delphidelphi-xe2vcl-styles

RichEdit control in Delphi XE2 won't show font colors under certain styles


I just noticed that in some of the new VCL styles in Delphi XE2, trying to change the color of text in a RichEdit control doesn't work. For example Smokey Quarts Kamri and Carbon will only show text in black, while in Cobalt XEMedia the font color can be changed. This is the code I used to change the font color (bold seems to work in all styles)

 memo.selStart:= length (text);
 memo.selLength:= 0;
 memo.SelAttributes.Color:= clRed;
 memo.SelAttributes.Style:= [fsBold];
 memo.selText := text;

Any ideas on how to change the font color on a RichEdit control while using Delphi XE2 styles?


Solution

  • It seems a VCL Styles bug, but you can fix this easily using a Style hook.

    uses
     Vcl.Forms,
     Vcl.Themes,
     Winapi.RichEdit;
    
    type
      TRichEditStyleHookFix = class(TScrollingStyleHook)
      strict private
        procedure EMSetBkgndColor(var Message: TMessage); message EM_SETBKGNDCOLOR;
      end;
    
    { TRichEditStyleHookFix }
    
    procedure TRichEditStyleHookFix.EMSetBkgndColor(var Message: TMessage);
    begin
      Message.LParam := ColorToRGB(StyleServices.GetStyleColor(scEdit));
      Handled := False;
    end;
    

    and use like so

      TStyleManager.Engine.RegisterStyleHook(TRichEdit, TRichEditStyleHookFix);
    

    enter image description here enter image description here enter image description here