📄 printstringsunit.pas
字号:
{******************************************************************************}
{ }
{ WinPrint - Print Spooler for DOS Programs }
{ }
{ Copyright (C) 2004 Przemyslaw Czerkas <przemekc@users.sourceforge.net> }
{ 2008 Mieczyslaw Nalewaj <namiltd@users.sourceforge.net> }
{ See GPL.TXT for copyright and license details. }
{ }
{******************************************************************************}
{******************************************************************************}
{ }
{ This file is part of WinPrint. }
{ }
{ WinPrint is free software; you can redistribute it and/or modify }
{ it under the terms of the GNU General Public License as published by }
{ the Free Software Foundation; either version 2 of the License, or }
{ (at your option) any later version. }
{ }
{ WinPrint is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU General Public License for more details. }
{ }
{ You should have received a copy of the GNU General Public License }
{ along with WinPrint; if not, write to the Free Software }
{ Foundation, Inc., 59 Temple Place, Suite 330, Boston, }
{ MA 02111-1307 USA }
{ }
{******************************************************************************}
unit PrintStringsUnit;
interface
uses
Windows, Classes, Graphics, Printers, ConversionUnit;
const
cMILTOINCH = 0.03937;
type
{ Prototype for a callback method that PrintString will call
when it is time to print a header or footer on a page. The
parameters that will be passed to the callback are:
aCanvas : the canvas to output on
aPageCount: page number of the current page, counting from 1
aTextRect : output rectangle that should be used. This will be
the area available between non-printable margin and
top or bottom margin, in device units (dots). Output
is not restricted to this area, though.
continue : will be passed in as True. If the callback sets it
to false the print job will be aborted. }
THeaderFooterProc = procedure(aCanvas: TCanvas;
aPageCount: Integer;
aTextrect: TRect;
var continue: Boolean) of object;
{+------------------------------------------------------------
| Function PrintStrings
|
| Parameters :
| lines:
| contains the text to print, already formatted into
| lines of suitable length. No additional wordwrapping
| will be done by this routine and also no text clipping
| on the right margin!
| leftmargin, topmargin, rightmargin, bottommargin:
| define the print area. Unit is inches, the margins are
| measured from the edge of the paper, not the printable
| area, and are positive values! The margin will be adjusted
| if it lies outside the printable area.
| linesPerInch:
| used to calculate the line spacing independent of font
| size.
| aFont:
| font to use for printout, must not be Nil.
| measureonly:
| If true the routine will only count pages and not produce any
| output on the printer. Set this parameter to false to actually
| print the text.
| OnPrintheader:
| can be Nil. Callback that will be called after a new page has
| been started but before any text has been output on that page.
| The callback should be used to print a header and/or a watermark
| on the page.
| OnPrintfooter:
| can be Nil. Callback that will be called after all text for one
| page has been printed, before a new page is started. The callback
| should be used to print a footer on the page.
| Returns:
| number of pages printed. If the job has been aborted the return
| value will be 0.
| Description:
| Uses the Canvas.TextOut function to perform text output in
| the rectangle defined by the margins. The text can span
| multiple pages.
| Nomenclature:
| Paper coordinates are relative to the upper left corner of the
| physical page, canvas coordinates (as used by Delphis Printer.Canvas)
| are relative to the upper left corner of the printable area. The
| printorigin variable below holds the origin of the canvas coordinate
| system in paper coordinates. Units for both systems are printer
| dots, the printers device unit, the unit for resolution is dots
| per inch (dpi).
| Error Conditions:
| A valid font is required. Margins that are outside the printable
| area will be corrected, invalid margins will raise an EPrinter
| exception.
| Created: 13.05.99 by P. Below
+------------------------------------------------------------}
function PrintStrings(Title: string;
lines: TStrings;
const CpNr: integer;
const PrinterId: integer;
const leftmargin,rightmargin,topmargin,bottommargin: single;
const orientation: TPrinterOrientation;
const linesPerInch: single;
const LinesPerPage: integer;
const SkipEmptyPages: boolean;
const aFont: TFont;
const Bitmap: TBitmap;
const leftlogo,toplogo: single;
const firstpageonlylogo: boolean;
const EOPcodes: TCharCodes;
measureonly: boolean;
OnPrintheader,OnPrintfooter: THeaderFooterProc): integer;
implementation
uses
Math;
function PrintStrings(Title: string;
lines: TStrings;
const CpNr: integer;
const PrinterId: integer;
const leftmargin,rightmargin,topmargin,bottommargin: single;
const orientation: TPrinterOrientation;
const linesPerInch: single;
const LinesPerPage: integer;
const SkipEmptyPages: boolean;
const aFont: TFont;
const Bitmap: TBitmap;
const leftlogo,toplogo: single;
const firstpageonlylogo: boolean;
const EOPcodes: TCharCodes;
measureonly: boolean;
OnPrintheader,OnPrintfooter: THeaderFooterProc): integer;
var
continuePrint: Boolean; { continue/abort flag for callbacks }
pagecount : Integer; { number of current page }
textrect : TRect; { output area, in canvas coordinates }
headerrect : TRect; { area for header, in canvas coordinates }
footerrect : TRect; { area for footes, in canvas coordinates }
lineheight : Integer; { line spacing in dots }
charheight : Integer; { font height in dots }
charheightco : Integer; { font height coefficient }
charstyleco : TFontStyles; { font style coefficient }
lineheightco : Integer; { line spacing in dots coefficient }
doublewidthco: Boolean; { double width coefficient }
textstart : Integer; { index of first line to print on
current page, 0-based. }
ll,tl : integer; {logo position}
{ Calculate text output and header/footer rectangles. }
procedure CalcPrintRects;
var
X_resolution: Integer; { horizontal printer resolution, in dpi }
Y_resolution: Integer; { vertical printer resolution, in dpi }
pagerect : TRect; { total page, in paper coordinates }
printorigin : TPoint; { origin of canvas coordinate system in
paper coordinates. }
{ Get resolution, paper size and non-printable margin from
printer driver. }
procedure GetPrinterParameters;
begin
with Printer.Canvas do
begin
X_resolution := GetDeviceCaps( handle, LOGPIXELSX );
Y_resolution := GetDeviceCaps( handle, LOGPIXELSY );
printorigin.X := GetDeviceCaps( handle, PHYSICALOFFSETX );
printorigin.Y := GetDeviceCaps( handle, PHYSICALOFFSETY );
pagerect.Left := 0;
pagerect.Right:= GetDeviceCaps( handle, PHYSICALWIDTH );
pagerect.Top := 0;
pagerect.Bottom := GetDeviceCaps( handle, PHYSICALHEIGHT );
end;
end;
{ Calculate area between the requested margins, paper-relative.
Adjust margins if they fall outside the printable area.
Validate the margins, raise EPrinter exception if no text area
is left. }
procedure CalcRects;
var
max: Integer;
begin
with textrect do
begin
{ Figure textrect in paper coordinates }
left:=round(leftmargin*X_resolution);
if (left<printorigin.x) then
left:=printorigin.x;
top:=round(topmargin*Y_resolution);
if (top<printorigin.y) then
top:=printorigin.y;
{ Printer.PageWidth and PageHeight return the size of the
printable area, we need to add the printorigin to get the
edge of the printable area in paper coordinates. }
right:=pagerect.right-round(rightmargin*X_resolution);
max:=Printer.PageWidth+printorigin.X;
if (right>max) then
right:=max;
bottom:=pagerect.bottom-round(bottommargin*Y_resolution);
max:=Printer.PageHeight+printorigin.Y;
if (bottom>max) then
bottom:=max;
{ Validate the margins. }
if (left>=right) or (top>=bottom) then
raise EPrinter.Create(RString(600));
end;
{ Convert textrect to canvas coordinates. }
OffsetRect( textrect, -printorigin.X, -printorigin.Y );
{ Build header and footer rects. }
headerrect := Rect( textrect.left, 0,
textrect.right, textrect.top );
footerrect := Rect( textrect.left, textrect.bottom,
textrect.right, Printer.PageHeight );
end;
begin
GetPrinterParameters;
CalcRects;
if (linesPerInch<>0) then
lineheight:=round(Y_resolution/linesPerInch)
else
if (LinesPerPage<>0) then
lineheight:=round((textrect.bottom-textrect.top)/LinesPerPage)
else
raise EPrinter.Create(
'PrintString: ilo滄 linii na cal i ilo滄 linii na stron
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -