Author: Mike Shkolnik
How can I set a some printer settings?
Answer:
At first, of course, you must open the printer device (as I described in previous
tip " to get a printer settings").
Now you can set the any settings (supported only, of course) in properties of
DevMode^ variable and add a "assigned" flag in DevMode^.dmFields.
After that you need call a SetPrinter procedure and unlock device.
View small example:
1 2 procedure SetPrinterSettings(FPrinter: TPrinter);
3 var4 FDevice: PChar;
5 FDriver: PChar;
6 FPort: PChar;
7 DeviceMode: THandle;
8 DevMode: PDeviceMode;
9 begin10 {to get a current printer settings}11 FPrinter.GetPrinter(FDevice, FDriver, FPort, DeviceMode);
12 {lock a printer device}13 DevMode := GlobalLock(DeviceMode);
14 15 {set a paper size as A4-Transverse}16 if ((DevMode^.dmFields and DM_PAPERSIZE) = DM_PAPERSIZE) then17 begin18 DevMode^.dmFields := DevMode^.dmFields or DM_PAPERSIZE;
19 DevMode^.dmPaperSize := DMPAPER_A4_TRANSVERSE;
20 end;
21 22 {set a paper source as Tractor bin}23 if ((DevMode^.dmFields and DM_DEFAULTSOURCE) = DM_DEFAULTSOURCE) then24 begin25 DevMode^.dmFields := DevMode^.dmFields or DM_DEFAULTSOURCE;
26 DevMode^.dmDefaultSource := DMBIN_TRACTOR;
27 end;
28 29 {set a Landscape orientation}30 if ((DevMode^.dmFields and DM_ORIENTATION) = DM_ORIENTATION) then31 begin32 DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;
33 DevMode^.dmOrientation := DMORIENT_LANDSCAPE;
34 end;
35 36 {set a printer settings}37 FPrinter.SetPrinter(FDevice, FDriver, FPort, DeviceMode);
38 39 {unlock a device}40 GlobalUnlock(DeviceMode);
41 end;
If you need to change the paper size to custom size for example, 100mm x 100mm, you
must assign the custom width and height to dmPaperWidth and dmPaperLength and
include the DM_PAPERWIDTH/DM_PAPERLENGTH flags to dmFields property:
42 43 DevMode^.dmPaperWidth := PaperSizeWidth;
44 DevMode^.dmPaperLength := PaperSizeHeight;
45 DevMode^.dmFields := DevMode^.dmFields or DM_PAPERWIDTH or DM_PAPERLENGTH;