2005. június 20., hétfő

Printing to a specific bin


Problem/Question/Abstract:

Needed routine to print output to a specific bin on a laser printer. The key to this routine is the DevMode structure, there are several other routines that could be written using this structure.

Answer:

procedure BinToPrintTo(BinNo: Integer);
var
  ADevice, ADriver, APort: string;
  ADeviceMode: THandle;
  DevMode: PDeviceMode;
begin
  SetLength(ADevice, 255);
  SetLength(ADriver, 255);
  SetLength(APort, 255);
  if ADeviceMode = 0 then
  begin
    Printer.PrinterIndex := Printer.PrinterIndex;
    Printer.GetPrinter(PChar(ADevice), PChar(ADriver),
      PChar(APort), ADeviceMode);
  end;
  if ADeviceMode <> 0 then
  begin
    DevMode := GlobalLock(ADeviceMode);
    try
      DevMode^.dmDefaultSource := BinNo;
    finally
      GlobalUnlock(ADeviceMode);
    end;
  end
  else
    raise Exception.Create('Could not set printer copies');
end;

The functionality to Query the bin numbers. Setting the bin number must follow the query of binnames and the assosiated number the following code  is a brief sample how it could be done.

var
  iPaperTrayCount: Integer;
  sBinNames: array of array[0..23] of Char;
  iBinValues: array of Smallint;
  iNumBins: Integer;
  sPrinterDev: string;

begin
  //  Get the device name
  sPrinterDev := TPrinterDevice(Printer.Printers.Objects[PrinterIndex]).Device;
  // Query the number of Papaerbins
  iNumBins := DeviceCapabilities(PChar sPrinterDev), '', DC_BINNAMES, nil, nil);

if iNumBins >= 0 then
begin
  // Create an array that holds the name of the paper bin
  // and a second to hold the needed Binnumbers

  SetLength(sBinNames, iNumBins);
  SetLength(iBinValues, iNumBins);

  // get the names
  DeviceCapabilities(PChar(sPrinterDev), nil, DC_BINNAMES, @sBinNames[0], nil);
  // get the numbers
  DeviceCapabilities(PChar(sPrinterDev), nil, DC_BINS, @iBinValues[0], nil);
end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése