Property changes in same print
How to enable printer property changes ( like paper tray, orientation, etc. ) between pages in the same print document in only six steps.
(The example at the end also shows how to switch paper trays...)
*** THE STEPS ***
Create a copy of Printers.pas and rename the copy to NewPrint.pas.
***DO NOT make these changes to Printers.pas, if you do you will get an error saying "Unable to find printers.pas" when you compile your application. (Well, that's the error I received...)***
Move NewPrint.pas to the Lib directory.
(Usually "C:\Program Files\Borland\Delphi 2.0\Lib" )
Change the UNIT NAME in NewPrint.pas
from:
unit Printers;
to:
unit NewPrint;
Add the following PUBLIC METHOD declaration to the TPrinter object definition in the Interface section of NewPrint.pas:
procedure NewPageDC(DM: PDevMode);
Add the following procedure to the Implementation section of NewPrint.pas:
procedure TPrinter.NewPageDC(DM: PDevMode);
begin
CheckPrinting(True);
EndPage(DC);
{Check to see if new device mode setting were passed}
if Assigned(DM) then
ResetDC(DC,DM^);
StartPage(DC);
Inc(FPageNumber);
Canvas.Refresh;
end;
Instead of adding "Printers" to the USES clause of your application, add "NewPrint".
EVERYTHING ELSE WORKS EXACTLY THE SAME (ie BeginDoc, EndDoc, NewPage, etc.) but you now have the capability of changing printer settings on the fly between pages WITHIN THE SAME PRINT DOCUMENT. (The example below shows how.)
Instead of calling:
Printer.NewPage;
call:
Printer.NewPageDC(DevMode);
Here is the small example (with bytes of code from other print altering routines I've gathered).
procedure TForm1.Button1Click(Sender: TObject);
var
ADevice, ADriver, APort: array [0..255] of char;
ADeviceMode: THandle;
DevMode: PDevMode;
begin
with Printer do begin
GetPrinter(ADevice,ADriver,APort,ADeviceMode);
SetPrinter(ADevice,ADriver,APort,0);
GetPrinter(ADevice,ADriver,APort,ADeviceMode);
DevMode := GlobalLock(ADeviceMode);
if not Assigned(DevMode) then ShowMessage('Can''t set printer.')
else begin
with DevMode^ do begin
{Put any other settings you want here}
dmDefaultSource := DMBIN_UPPER;
{these codes are listed in "Windows.pas"}
end;
GlobalUnlock(ADeviceMode);
SetPrinter(ADevice,ADriver,APort,ADeviceMode);
end;
end;
Printer.BeginDoc;
Printer.Canvas.TextOut(50,50,'This page is printing from the UPPER PAPER TRAY.');
with DevMode^ do begin
{Put any other settings you want here}
dmDefaultSource := DMBIN_LOWER;
{these codes are listed in "Windows.pas"}
end;
Printer.NewPageDC(DevMode);
Printer.Canvas.TextOut(50,50,'This page is printing from the LOWER PAPER TRAY.');
Printer.EndDoc;
end;
|