procedure TCmdForm.ViewPageClick(Sender: TObject);
var
fr: TFormatRange;
dc: THandle;
ox, oy, w, h: LongInt;
cpMin: LRESULT;
c: Integer;
PgWide, PgHigh, PgSide, PgHead, InchW, InchH: double;
PixHigh, PixWide: integer;
begin
PgWide:= 8.5; PgHigh:= 11.0; PgSide:= 1.0; PgHead:= 1.0;
// setup the TextArea dimensions
PaperArea.top:= 40;
PaperArea.height:= round(PgHigh * Screen.PixelsPerInch) div 2;
PaperArea.width:= round(PgWide * Screen.PixelsPerInch) div 2;
PaperArea.left:= (PageMemo.width - PaperArea.width) div 2;
InchH:= PgHigh/2 - PgHead; // Phead is half of head and tail
InchW:= PgWide/2 - PgSide; // Pside is half of left and right
PixHigh:= round(InchH * Screen.PixelsPerInch); // standard = 96 per inch
PixWide:= round(InchW * Screen.PixelsPerInch); // twips are 1440 per inch
// setup the printable region
TextArea.Top:= Screen.PixelsPerInch div 2; // 48; // half of screen pixels per inch
TextArea.height:= PixHigh;
TextArea.width:= PixWide;
TextArea.left:= Screen.PixelsPerInch div 2; // 48; // half of screen pixels per inch
TextArea.Picture.Bitmap.SetSize(round(PixWide * 2), round(PixHigh * 2));
PaperArea.visible:= true;
// restart the canvas
TextArea.Canvas.Brush.Color:= clWindow;
TextArea.Canvas.FillRect(0, 0, TextArea.Canvas.ClipRect.Right, TextArea.Canvas.ClipRect.Bottom);
TextArea.Canvas.MoveTo(0, 0);
dc:= TextArea.Canvas.Handle;
ox:= 0; //GetDeviceCaps(dc, PHYSICALOFFSETX);
oy:= 0; //GetDeviceCaps(dc, PHYSICALOFFSETY);
w:= XPixToTwips(PixWide * 2); //GetDeviceCaps(dc, PHYSICALWIDTH);
h:= YPixToTwips(PixHigh * 2); //GetDeviceCaps(dc, PHYSICALHEIGHT);
fr.hdc:= dc; // fr._hdc := dc; **on another system**
fr.hdcTarget:= dc;
fr.rc.Left:= ox;
fr.rc.Right:= ox+w;
fr.rc.Top:= oy;
fr.rc.Bottom:= oy+h;
SendMessage(PageMemo.Handle, EM_SETSEL, 0, -1);
SendMessage(PageMemo.Handle, EM_EXGETSEL, 0, LParam(@fr.chrg));
SendMessage(PageMemo.Handle, EM_SETSEL, 0, 0);
c:= 1;
repeat
cpMin:= SendMessage(PageMemo.Handle, EM_FORMATRANGE, 1, LParam(@fr));
if cpMin<fr.chrg.cpMin then
break;
fr.chrg.cpMin:= cpMin;
inc(c);
until c>=1000; { Not forever! }
SendMessage(PageMemo.Handle, EM_FORMATRANGE, 0, 0);
TextArea.Picture.Bitmap.Canvas.Changed;
end;