Recent

Author Topic: Change font colour when saving RTF file from TRichMemo  (Read 367 times)

DelphiDinosaur

  • New Member
  • *
  • Posts: 17
Change font colour when saving RTF file from TRichMemo
« on: May 23, 2025, 12:36:49 pm »
I have a terminal program displaying colour coded strings in a TRichMemo control.

I have the TRichMemo color property set to black and display text in white, yellow or green depending on source (status, input, output).

However, when saving the text to a .RTF file the white text is output white - meaning it's invisible unless highlighted in the RTF viewer.

Is there a simple way of changing the white coloured text to black without changing yellow and green characters?

I'm currently using GetStyleRange, GetTextAttributes and SetTextAttributes but wonder if there's a better way:
Code: Pascal  [Select][+][-]
  1. var
  2. i: integer
  3. FP: TFontParams;
  4. RangeStart: integer;
  5. RangeLen: integer;
  6. res: boolean;
  7.  
  8. begin
  9. i := 0;
  10. while i < Length(MyRichMemo.Text) do
  11. begin
  12.    res := MyRichMemo.GetStyleRange(i, RangeStart, RangeLen);
  13.    if (res = true) then
  14.    begin
  15.       MyRichMemo.GetTextAttributes(i, FP);
  16.       if (FP.Color = clWhite) then
  17.       begin
  18.          FP.Color := clBlack;
  19.          MyRichMemo.SetTextAttributes(RangeStart, RangeLen, FP);
  20.       end;
  21.       i := i + RangeLen;
  22.    end
  23.    else
  24.    begin
  25.       i := i + 1;
  26.    end;  
  27. end;
  28.  

rvk

  • Hero Member
  • *****
  • Posts: 6777
Re: Change font colour when saving RTF file from TRichMemo
« Reply #1 on: May 23, 2025, 12:58:47 pm »
Without using the richmemo itself... you can also just change the RTF content.

If you look at the RTF source of the resulting output file, you'll something like
Code: Text  [Select][+][-]
  1. {\rtf1\ansi
  2. {\colortbl;\red255\green255\blue255;}
  3. \cf1 This is white text.
  4. }
You can just change the colors in the \colortbl to reflect a darker color.
(the white one will be the 255,255,255 color)
Change it to 0,0,0 - For example {\colortbl ;\red0\green0\blue0;} for black.
There is no need to change the complete RTF.

When loading back in your program... you might want to change it back to white.
« Last Edit: May 23, 2025, 01:00:24 pm by rvk »

 

TinyPortal © 2005-2018