Change text color before printing

General TRichView support forum. Please post your questions here
Post Reply
AndreMurta
Posts: 8
Joined: Tue Nov 25, 2014 5:55 pm

Change text color before printing

Post by AndreMurta »

I'm creating a text editor based in the ActionTest demo.

I have included to the end customer the possibility to insert data fields stored in a TListBox control (lstGlobalVars), using the InsertStringTag function. Each entry in lstGlobalsVars is actually a key to a TDictionary class which stores the values related to the key indexes.

If the user double click the listbox control, the following function is called:

Code: Select all

const
  TEXT_BOLD = 1;
  TEXT_ITALIC = 2;
  TEXT_UNDERLINE = 3;
  TEXT_APPLYFONTNAME = 4;
  TEXT_APPLYFONT = 5;
  TEXT_APPLYFONTSIZE = 6;
  TEXT_COLOR = 7;
  TEXT_BACKCOLOR = 8;
  TEXT_FIELDPROTECT = 9;

procedure TfLstGlobalVars.lstGlobalVarsDblClick(Sender: TObject);
var
  idx: integer;
begin
  idx := lstGlobalVars.ItemIndex;

  with fMain do
  begin
    SRichViewEdit1.RichViewEdit.ApplyStyleConversion(TEXT_FIELDPROTECT);
    SRichViewEdit1.RichViewEdit.InsertStringTag(
      DictVars.Items[lstGlobalVars.Items[idx]],
      lstGlobalVars.Items[idx]
    );
    SetFocus;
  end;
end;
I have the following procedure vinculated to the OnStyleConversion event in the TSRichViewEdit control:

Code: Select all

procedure TfMain.SRichViewEdit1StyleConversion(Sender: TCustomRichViewEdit; StyleNo, UserData: Integer; AppliedToText: Boolean; var NewStyleNo: Integer);
var
  FontInfo: TFontInfo;
begin
  FontInfo := TFontInfo.Create(nil);
  try
    FontInfo.Assign(Sender.Style.TextStyles[StyleNo]);
    case UserData of
      TEXT_FIELDPROTECT:
      begin
        FontInfo.Protection := [rvprStyleProtect, rvprModifyProtect, rvprStyleSplitProtect, rvprConcateProtect, rvprRVFInsertProtect, rvprDoNotAutoSwitch];
        FontInfo.BackColor := clSilver;
      end;
    end;
    NewStyleNo := Sender.Style.TextStyles.FindSuchStyle(StyleNo, FontInfo,
                                                       RVAllFontInfoProperties);
    if NewStyleNo=-1 then
    begin
      Sender.Style.TextStyles.Add;
      NewStyleNo := Sender.Style.TextStyles.Count-1;
      Sender.Style.TextStyles[NewStyleNo].Assign(FontInfo);
      Sender.Style.TextStyles[NewStyleNo].Standard := False;
    end;
  finally
    FontInfo.Free;
  end;
end;
So, when the user double click the listbox, the field is inserted with "clSilver" as the text background color, the field contents are protected against editing.

This alternative background color is good on screen, but I would like to change it back to clNone (or clWindow) when the document is printed. I have no clue about how to do it. Is it possible?
Sergey Tkachenko
Site Admin
Posts: 17309
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You can try using OnApplyStyleColor event.
Create a global variable PrintingDoc: Boolean, assign True to it while printing.

Assign this code to SRV.RichViewEdit.Style.OnApplyStyleColor:

Code: Select all

procedure TForm3.RVStyleApplyStyleColor(Sender: TRVStyle; Canvas: TCanvas;
  StyleNo: Integer; DrawState: TRVTextDrawStates; ApplyTo: TRVColoredParts;
  var DoDefault: Boolean);
begin
  if PrintingDoc then begin
    Sender.TextStyles[StyleNo].ApplyColor(Canvas, Sender, DrawState, True, rvcmPrinterColor);
    if rvprStyleProtect in Sender.TextStyles[StyleNo].Protection then begin
      Canvas.Brush.Color := clNone;
      Canvas.Brush.Style := bsClear;
    end;
    DoDefault := False;
  end;
end;
AndreMurta
Posts: 8
Joined: Tue Nov 25, 2014 5:55 pm

Post by AndreMurta »

Your solution worked, but I had to change the way to use it, since I could not find an event triggered at the end of the document printing, in order to setup PrintingDoc value back to false.
I just created a menu item called "Highlight Fields" which can be checked/unchecked. Depending on the item status I call SVR.RichViewEdit.Repaint to highlight or not the fields. It's not exactly what I expected but it works.
Thanks a lot. Your suite of components is just great.
Post Reply