Mapped files and TScrollBar

 Sorry if this is long, but I don't know how to make it any
shorter.

 I'm trying to learn how to put the pages of a Memory Mapped file
into a TMemo. I've created the file using CreateFile,
CreateFileMapping, and MapViewofFile and can show it in increments
of 4096 bytes (CBufSize) in Memo1 like this:

procedure TForm1.btDown_1pageClick(Sender: TObject);
begin
   Inc(FText,CBufSize); //  Dec(FText,CBufSize);
   Move(FText^, buffer[1], CBufSize);
   Memo1.Lines.Text := buffer;
end;

 What is driving me crazy is trying to coordinate the contents of
Memo1 with a TScrollBar. After opening the file, I set the
scrollbar like this (FTotSize is the file size). What I'm trying to
do is get the Position unit to represent one 'page' the size of
CBufSize so that the ScrollBar will load a new page of the Mapped
file with each change in position:

{...}
MinHeight := GetSystemMetrics(SM_CYVTHUMB);
with SBar1 do
begin
  Min := 0;
  if FTotSize > CBufSize then Max := (FTotsize div CBufSize);
  TrackHeight := Memo1.Height - 2 * GetSystemMetrics(SM_CYVSCROLL);
  PageSize := TrackHeight div (Max - Min + 1);
  if PageSize < MinHeight then PageSize := MinHeight;
{...}

procedure TForm1.SBar1Scroll(Sender: TObject; ScrollCode:
TScrollCode; var ScrollPos: Integer);
begin
  case ScrollCode of
  scLineDown: Memo1.Perform(EM_LineScroll, 0, + 1);
  scLineUp  : Memo1.Perform(EM_LineScroll, 0, - 1);
  scPageUp  : Memo1.Perform(EM_LineScroll, 0, 0 - SBar1.PageSize);
  scPageDown: Memo1.Perform(EM_LineScroll, 0, 0 + SBar1.PageSize);
  scTrack   :
   begin
     if (SBar1.Position > oldpos) and (FTotSize > CBufSize) then
        Inc(FText,CBufSize)
   else if (SBar1.Position < oldpos) and (FTotSize > CBufSize) then
        Dec(FText,CBufSize);
        Move(FText^, buffer[1], CBufSize);
        Label1.Caption := 'SBar1.Max: ' + inttostr(SBar1.Max);
        Label2.Caption := 'SBar1.Position: '
+inttostr(SBar1.Position);
   end;
   scEndScroll: Memo1.Lines.Text := buffer;
   scPosition: oldpos := SBar1.Position;
  end; //case
end;

I've got at least three problems:

1) The pages are placed into Memo1 correctly, but labels I've
placed on the form show me that SBar1.Position doesn't have any
relationship to the top and bottom positions. If I slide it to the
top, Position may be 3, or 7 or 13 and at the bottom I get the same
erratic positions.

2) The PgUp or PgDown keys move through the current page as they
should, but clicking on the LineUp or LineDown arrows moves through
a different amount of text.

3) If I move to the bottom of the page using either the keys or the
scrollbar arrows, I don't know what value to use to tell the
program to increment the buffer to load the following page.

Since this is all new to me, perhaps my basic approach is wrong.
I'd appreciate any pointers that anyone could give me on this. TIA,

Brad Blanchard