unit demoUnit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TfrmDemo = class(TForm)
    memo: TRichEdit;
    procedure memoChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure UpdateSyntax;
  end;

var
  frmDemo: TfrmDemo;

implementation

uses psvCPlusPlus;



{$R *.DFM}

procedure TfrmDemo.UpdateSyntax;
var
  TempMS: TMemoryStream;
  FSyntax: TpsvCppRTF;
  pos, top: Integer;
  OnChange: TNotifyEvent;
begin

  if (Length(memo.Text) <= 0) then
    exit;

  pos := memo.selstart;
  top := SendMessage(memo.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
  OnChange := memo.OnChange;
  TempMS := TMemoryStream.Create;
  memo.OnChange := nil;
  try
    FSyntax := TpsvCppRTF.Create;
    try
      try
        FSyntax.SetText(Memo.Text);
        FSyntax.ConvertToRTFStream(TempMS);
        TempMS.Position := 0;
        memo.PlainText := False;
        memo.Lines.BeginUpdate;
        memo.Lines.LoadFromStream(TempMS);
        SendMessage(memo.Handle, EM_LINESCROLL, 0, top);
        memo.Lines.EndUpdate;
      finally
        FSyntax.Free;
      end;
    except
      memo.SelAttributes := memo.DefAttributes;
    end;
  finally
    memo.PlainText := True;
    memo.SelStart := Pos;
    TempMS.Free;
    memo.OnChange := OnChange;
  end;
end;

procedure TfrmDemo.memoChange(Sender: TObject);
begin
  UpdateSyntax;
end;

procedure TfrmDemo.FormCreate(Sender: TObject);
begin
  UpdateSyntax;
end;

end.
