{
  This demo draws rectangles around all items.
  There are two options:
  - drawing rectangles around each item part (when a text item occupies
    several lines, each line is a part of this item)
  - drawing rectangles around the whole items
}

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    SRichViewEdit1: TSRichViewEdit;
    Panel1: TPanel;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    procedure Paint1(Sender: TObject; PageNo: Integer;
  PageRect, R: TRect; Canvas: TCanvas; Prepaint, Printing: Boolean);
    procedure Paint2(Sender: TObject; PageNo: Integer;
  PageRect, R: TRect; Canvas: TCanvas; Prepaint, Printing: Boolean);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
{------------------------------------------------------------------------------}
// OnPaint, option 1: drawing around each part of each item
procedure TForm1.Paint1(Sender: TObject; PageNo: Integer;
  PageRect, R: TRect; Canvas: TCanvas; Prepaint, Printing: Boolean);
var
  i: Integer;
  FirstItemNo, LastItemNo, Offs, ItemPart: Integer;
  FirstPageNo, LastPageNo : Integer;
  ItemRect: TRect;
begin
  // Receiving a range of items on the page
  SRichViewEdit1.GetPageStartItemNo(PageNo, FirstItemNo, Offs);
  SRichViewEdit1.GetPageLastItemNo(PageNo, LastItemNo, Offs);

  Canvas.Pen.Color := clRed;
  Canvas.Brush.Style := bsClear;
  // drawing for all items on the page...
  for i := FirstItemNo to LastItemNo do begin
    ItemPart := 0;
    // drawing for all parts of the i-th item on the page
    while SRichViewEdit1.GetItemBounds100(
      SRichViewEdit1.RichViewEdit.RVData, i, ItemRect,
      FirstPageNo, LastPageNo, ItemPart) do begin
      if FirstPageNo = PageNo then begin
        OffsetRect(ItemRect, PageRect.Left, PageRect.Top);
        Canvas.Rectangle(ItemRect);
      end;
      inc(ItemPart);
    end;
  end;
end;
{------------------------------------------------------------------------------}
// OnPaint, option 2: drawing around each item
procedure TForm1.Paint2(Sender: TObject; PageNo: Integer;
  PageRect, R: TRect; Canvas: TCanvas; Prepaint, Printing: Boolean);
var
  i: Integer;
  FirstItemNo, LastItemNo, Offs, ItemPart: Integer;
  FirstPageNo, LastPageNo : Integer;
  ItemRect, ItemPartRect : TRect;
begin
  // Receiving a range of items on the page
  SRichViewEdit1.GetPageStartItemNo(PageNo, FirstItemNo, Offs);
  SRichViewEdit1.GetPageLastItemNo(PageNo, LastItemNo, Offs);

  Canvas.Pen.Color := clGreen;
  Canvas.Brush.Style := bsClear;
  // drawing for all items on the page...
  for i := FirstItemNo to LastItemNo do begin
    ItemPart := 0;
    ItemRect := Bounds(0, 0, 0, 0);
    // combining rectangles for all parts of the i-th item on this page
    while SRichViewEdit1.GetItemBounds100(
      SRichViewEdit1.RichViewEdit.RVData, i, ItemPartRect,
      FirstPageNo, LastPageNo, ItemPart) do begin
      if FirstPageNo = PageNo then
        UnionRect(ItemRect, ItemRect, ItemPartRect);
      inc(ItemPart);
    end;
    // drawing this combined rectangle
    OffsetRect(ItemRect, PageRect.Left, PageRect.Top);
    Canvas.Rectangle(ItemRect);
  end;
end;
{------------------------------------------------------------------------------}
// Choosing option 1
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  SRichViewEdit1.OnPaintPage := Paint1;
  SRichViewEdit1.UpdateBuffer;
end;
{------------------------------------------------------------------------------}
// Choosing option 2
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  SRichViewEdit1.OnPaintPage := Paint2;
  SRichViewEdit1.UpdateBuffer;
end;

end.
