procedure TForm1.LeftMarginEditKeyPress(Sender: TObject; var Key: Char); begin Ifnot (Key in ['0'..'9',#9,DecimalSeparator]) Then Key := #0; end;
procedure TForm1.FormCreate(Sender: TObject);
var S: String; procedure
loadpreviewtext; var sl: TStringlist; begin sl:= Tstringlist.Create;
try sl.Loadfromfile( Extractfilepath( application.exename )+'printpreview.pas'); PreviewText := sl.Text;
finally sl.free
end; end; begin // Initialize the margin edits with a margin
of 0.75 inch S:= FormatFloat('0.00',0.75); LeftMarginEdit.Text := S;
TopMarginEdit.Text := S;
RightMarginEdit.Text := S;
BottomMarginEdit.Text := S;
// Initialize the orientation radio
group If Printer.Orientation = poPortrait Then OrientationRGroup.ItemIndex
:= 0 Else OrientationRGroup.ItemIndex
:= 1;
// load test text for display LoadPreviewtext; end;
procedure TForm1.PreviewPaintboxPaint(Sender: TObject);
var pagewidth, pageheight: Double; // printer page
dimension in inch printerResX, printerResY: Integer; // printer resolution in
dots/inch minmarginX,
minmarginY: Double;
// nonprintable margin in inch outputarea: TRect;
// print area in 1/1000 inches scale: Double;
// conversion factor, pixels per 1/1000 inch
procedure
InitPrintSettings; function GetMargin( S: String; inX: Boolean ):Double;
begin Result := StrToFloat(S);
if InX
thenbegin if Result ‹ minmarginX then Result := minmarginX; end elsebegin if Result ‹ minmarginY then Result := minmarginY; end;
end; begin printerResX := GetDeviceCaps( printer.handle, LOGPIXELSX );
printerResY := GetDeviceCaps( printer.handle, LOGPIXELSY );
pagewidth := GetDeviceCaps( printer.handle, PHYSICALWIDTH ) / printerResX; pageheight := GetDeviceCaps( printer.handle, PHYSICALHEIGHT) / printerResY;
minmarginX := GetDeviceCaps( printer.handle, PHYSICALOFFSETX)/ printerResX; minmarginY := GetDeviceCaps( printer.handle, PHYSICALOFFSETY)/ printerResY;
procedure ScaleCanvas(
Canvas: TCanvas; widthavail, heightavail: Integer ); var needpixelswidth, needpixelsheight: Integer; // dimensions of preview at current zoom factor in
pixels orgpixels:
TPoint; // origin of preview
in pixels begin // set up a coordinate system
for the canvas that uses 1/1000 inch as unit,
// honors the zoom factor and
maintains the MM_TEXT orientation of the // coordinate axis (origin in
top left corner, positive Y axis points // down. scale := Screen.PixelsPerInch / 1000;
// Apply zoom factor scale := scale * StrToInt(Zoomedit.text) / 100; // figure out size of preview needpixelswidth := Round(pagewidth * 1000 * scale);
needpixelsheight := Round(pageheight * 1000 * scale);
if
needpixelswidth ›= widthavail
then orgpixels.X
:= 0 else orgpixels.X
:= (widthavail - needpixelswidth) div 2;
if needpixelsheight
›= heightavail then orgpixels.Y
:= 0 else orgpixels.Y
:= (heightavail - needpixelsheight) div 2;