unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    SRichViewEdit1: TSRichViewEdit;
    Panel1: TPanel;
    cmbFrame: TComboBox;
    cmbZoom: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cmbFrameChange(Sender: TObject);
    procedure SRichViewEdit1Paint(Sender: TSRichViewEdit; Canvas: TCanvas;
      Prepaint: Boolean; PaintRect: TRect);
    procedure cmbZoomChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Metafile : TMetafile;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Metafile := TMetafile.Create;
  cmbZoom.ItemIndex := 1;
  cmbZoomChange(nil);
  cmbFrame.ItemIndex := 0;
  cmbFrameChange(nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Metafile.Free;
end;

procedure TForm1.cmbFrameChange(Sender: TObject);
var FileName, Path: String;
begin
  FileName := IntToStr(cmbFrame.ItemIndex + 1) + '.emf';
  Path := ExtractFilePath(Application.ExeName);
  if FileExists(Path+FileName) then
    Metafile.LoadFromFile(Path+FileName)
  else
    Metafile.LoadFromFile(Path+'..\'+FileName);  
  SRichViewEdit1.UpdateBuffer;
end;
{------------------------------------------------------------------------------}
// Drawing metafile as a page border.
// We cannot use OnPaintPage because we draw outside the page (partially)

procedure TForm1.SRichViewEdit1Paint(Sender: TSRichViewEdit;
  Canvas: TCanvas; Prepaint: Boolean; PaintRect: TRect);
var
     OldInfo : TSRVZoomInfo;
     ZoomPercent : Single;
     OldRect, PageRect : TRect;
     PageNo : Integer;
     hRegion: HRGN;
begin
  If not Prepaint then exit;
  // applying the clipping rectangle
  GetClipBox(Canvas.Handle, OldRect);
  hRegion := CreateRectRgn(PaintRect.Left, PaintRect.Top, PaintRect.Right, PaintRect.Bottom);
  SelectClipRgn(Canvas.Handle, hRegion);
  DeleteObject(hRegion);
  // scaling all subsequent drawing by ZoomPercent
  ZoomPercent := SRichViewEdit1.ViewProperty.ZoomPercent * 0.01;  
  OldInfo := SRichViewEdit1.ZoomCanvas(Canvas, ZoomPercent, ZoomPercent);
  // drawing on all visible pages
  for PageNo := SRichViewEdit1.GetFirstVisiblePage to SRichViewEdit1.GetLastVisiblePage do begin
    PageRect := SRichViewEdit1.GetPageClientRect(PageNo);
    // zooming page rectangle by 1/ZoomPercent to compensate zooming made by ZoomCanvas
    PageRect := SRichViewEdit1.ZoomRectOut(PageRect, ZoomPercent);
    // increasing page rect by 20 (independent of zooming)
    InflateRect(PageRect, 20, 20);
    // drawing metafile
    Canvas.StretchDraw(PageRect, Metafile);
    Canvas.Handle;
  end;
  // restore the original scaling
  SRichViewEdit1.RestoreCanvasZoom(Canvas, OldInfo);
  // restore the original clipping region
  with OldRect do
    hRegion := CreateRectRgn(Left, Top, Right, Bottom);
  SelectClipRgn(Canvas.Handle, hRegion);
  DeleteObject(hRegion);
end;

procedure TForm1.cmbZoomChange(Sender: TObject);
begin
  If cmbZoom.ItemIndex = 0 then
    SRichViewEdit1.ViewProperty.ZoomMode := rvzmPageWidth
  else
    SRichViewEdit1.ViewProperty.ZoomMode := rvzmFullPage;
end;

end.
