2006. augusztus 22., kedd

Set a printer settings


Problem/Question/Abstract:

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:

procedure SetPrinterSettings(FPrinter: TPrinter);
var
  FDevice: PChar;
  FDriver: PChar;
  FPort: PChar;
  DeviceMode: THandle;
  DevMode: PDeviceMode;
begin
  {to get a current printer settings}
  FPrinter.GetPrinter(FDevice, FDriver, FPort, DeviceMode);
  {lock a printer device}
  DevMode := GlobalLock(DeviceMode);

  {set a paper size as A4-Transverse}
  if ((DevMode^.dmFields and DM_PAPERSIZE) = DM_PAPERSIZE) then
  begin
    DevMode^.dmFields := DevMode^.dmFields or DM_PAPERSIZE;
    DevMode^.dmPaperSize := DMPAPER_A4_TRANSVERSE;
  end;

  {set a paper source as Tractor bin}
  if ((DevMode^.dmFields and DM_DEFAULTSOURCE) = DM_DEFAULTSOURCE) then
  begin
    DevMode^.dmFields := DevMode^.dmFields or DM_DEFAULTSOURCE;
    DevMode^.dmDefaultSource := DMBIN_TRACTOR;
  end;

  {set a Landscape orientation}
  if ((DevMode^.dmFields and DM_ORIENTATION) = DM_ORIENTATION) then
  begin
    DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;
    DevMode^.dmOrientation := DMORIENT_LANDSCAPE;
  end;

  {set a printer settings}
  FPrinter.SetPrinter(FDevice, FDriver, FPort, DeviceMode);

  {unlock a device}
  GlobalUnlock(DeviceMode);
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:

DevMode^.dmPaperWidth := PaperSizeWidth;
DevMode^.dmPaperLength := PaperSizeHeight;
DevMode^.dmFields := DevMode^.dmFields or DM_PAPERWIDTH or DM_PAPERLENGTH;

Nincsenek megjegyzések:

Megjegyzés küldése