Question

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?

Was it helpful?

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

OTHER TIPS

Past TStyleManager.Engine.RegisterStyleHook(TRichEdit, TRichEditStyleHookFix); in your *.dpr file

eq:

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  TStyleManager.TrySetStyle('Carbon');
  TStyleManager.Engine.RegisterStyleHook(TRichEdit, TRichEditStyleHookFix);
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top