Recent

Author Topic: Setting color of individual characters in TSynEdit  (Read 3090 times)

simone

  • Hero Member
  • *****
  • Posts: 648
Setting color of individual characters in TSynEdit
« on: January 02, 2025, 07:06:38 pm »
I need to set foreground, background and border colors for each character in TSynEdit. As far as I know, there is no native way to achieve the above, so I wrote a small extension, with the method

procedure SetColors(const StartPoint, EndPoint : TPoint; const ForegroundColor, BackgroundColor, BorderColor : TColor);

shown in the following code.

Code: Pascal  [Select][+][-]
  1. unit unit1;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, Forms, Controls, Graphics, FGL,
  8.   SynEdit, SynEditMarkupSelection, SynEditPointClasses, SynEditMarkup;
  9.  
  10. type
  11.  
  12.   { TSynEditSelectionList }
  13.  
  14.   TSynEditSelectionList=specialize TFPGObjectList<TSynEditSelection>;
  15.  
  16.   { TSynEdit }
  17.  
  18.   TSynEdit = class(SynEdit.TSynEdit)
  19.     private
  20.       aMarkupManager: TSynEditMarkupManager;
  21.       Blocks  : TSynEditSelectionList;
  22.     public
  23.       constructor Create(AOwner: TComponent); override;
  24.       destructor Destroy; override;
  25.       procedure SetColors(const StartPoint, EndPoint : TPoint; const ForegroundColor, BackgroundColor, BorderColor : TColor);
  26.     end;
  27.  
  28.   { TForm1 }
  29.  
  30.   TForm1 = class(TForm)
  31.     SynEdit: TSynEdit;
  32.     procedure FormCreate(Sender: TObject);
  33.   private
  34.  
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.  
  40. implementation
  41.  
  42. { TSynEdit }
  43.  
  44. constructor TSynEdit.Create(AOwner: TComponent);
  45. begin
  46.   inherited Create(AOwner);
  47.   aMarkupManager := TSynEditMarkupManager(MarkupMgr);
  48.   Blocks:=TSynEditSelectionList.Create;
  49. end;
  50.  
  51. destructor TSynEdit.Destroy;
  52. begin
  53.   Blocks.Free;
  54.   inherited Destroy;
  55. end;
  56.  
  57. procedure TSynEdit.SetColors(const StartPoint, EndPoint: TPoint; const ForegroundColor, BackgroundColor, BorderColor: TColor);
  58. var
  59.   TmpBlock  : TSynEditSelection;
  60.   TmpMarkup : TSynEditMarkupSelection;
  61. begin
  62.   TmpBlock:=TSynEditSelection.Create(ViewedTextBuffer,False);
  63.   with TmpBlock do
  64.     begin
  65.       InvalidateLinesMethod:=@InvalidateLines;
  66.       StartLineBytePos:=StartPoint;
  67.       EndLineBytePos:=EndPoint;
  68.     end;
  69.   TmpMarkup:=TSynEditMarkupSelection.Create(self,TmpBlock);
  70.   with TmpMarkup do
  71.     begin
  72.       MarkupInfo.FrameColor:=BorderColor;
  73.       MarkupInfo.Background:=BackgroundColor;
  74.       MarkupInfo.Foreground:=ForegroundColor;
  75.       Enabled:=True;
  76.     end;
  77.   aMarkupManager.AddMarkUp(TmpMarkup);
  78.   Blocks.Add(TmpBlock);
  79. end;
  80.  
  81. {$R *.lfm}
  82.  
  83. { TForm1 }
  84. procedure TForm1.FormCreate(Sender: TObject);
  85. begin
  86.   SynEdit.Lines.Add('012345678901234567890123456789');
  87.   SynEdit.Lines.Add('012345678901234567890123456789');
  88.   SynEdit.Lines.Add('012345678901234567890123456789');
  89.   SynEdit.SetColors(Point(5,1),Point(8,1),clBlue,clGray,clRed);
  90. end;
  91.  
  92. end.
  93. end;

Since I have a moderate knowledge of SynEdit, in sharing the code for the benefit of any interested member, I ask you if there is a better way to achieve the same result.

Thanks in advance.
« Last Edit: January 02, 2025, 07:11:14 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

LV

  • Full Member
  • ***
  • Posts: 239
Re: Setting color of individual characters in TSynEdit
« Reply #1 on: January 03, 2025, 02:55:05 pm »
I tried it, and it's great! You can also change the font style, such as making it bold (TSynPositionHighlighter).

https://gitlab.com/freepascal.org/lazarus/lazarus/-/tree/a5486f6a71e654f733c5731004e2c7d316458598/examples/SynEdit/SynPositionHighlighter

 

TinyPortal © 2005-2018