- Code: Select all
procedure SetPaperBin( PrtrName: string; Bin: word );
var
hndPrinter: Thandle;
BuffSize: integer;
PtrPrtrInfo2: PPrinterInfo2;
begin
OpenPrinter( PChar( PrtrName ), hndPrinter, nil );
{get size of buffer for printer info 2}
GetPrinter( hndPrinter, 2, nil, 0, @BuffSize );
PtrPrtrInfo2 := AllocMem( BuffSize );
{get printer info 2 ...}
GetPrinter( hndPrinter, 2, PtrPrtrInfo2, BuffSize, @BuffSize );
{... change bin if selected bin <> current bin}
if ( PtrPrtrInfo2^.pDevMode^.dmDefaultSource <> Bin ) then
begin
PtrPrtrInfo2^.pDevMode^.dmDefaultSource := Bin;
WinSpool.SetPrinter( hndPrinter, 2, PtrPrtrInfo2, 0 );
end;
ClosePrinter( hndPrinter );
FreeMem( PtrPrtrInfo2 );
end;
This code adds the tray codes to a stringlist
- Code: Select all
procedure GetDriverInfo(Sender : TObject; BinInfo : tStringList); // call this to get the driver information
var
I: Integer;
begin
MemAllocations_Open(Sender);
if not assigned(fPrinter) then exit;
// this call returns the FDevice string of the selected printer
Printer.GetPrinter(FDevice, FDriver, FPort, DeviceMode);
DevMode := GlobalLock(DeviceMode);
GetMem(Driver_info_2, 1000);
try
OpenPrinter(FDevice, hPrinter, nil);
try
GetPrinterDriver(hPrinter, nil, 2, Driver_info_2, 1000, Retrieved);
except
end;
except
end;
DeviceCapabilities(FDevice, FPort, DC_BINS, CapBuffer, nil);
// Bin names
NumCaps := DeviceCapabilities(FDevice, FPort, DC_BINNAMES, CapBuffer, nil);
copymemory( pointer(binnames), capbuffer, 2400);
SetLength(BinCodes, NumCaps);
Fillchar(Pointer(BinCodes)^, NumCaps * Sizeof(word), #0);
// Bin codes
NumCaps := DeviceCapabilities(FDevice, FPort, DC_BINS, PChar(BinCodes), nil);
// write valid bin codes to the string list
for I := 0 to NumCaps - 1 do
begin
if trim(binnames[i]) <> '' then begin
BinInfo.Add(binnames[i] + format( ': (%d)', [BinCodes[I]]));
end;
end;
// clean it all up
FreeMem(Driver_info_2, 255);
globalunlock( devicemode);
MemAllocations_Close(Sender);
end;
This selects a tray ok, but it does not change the Printer.Orientation.
- Code: Select all
procedure ChangePrinter(ToBin: Integer; ToOrientation: TPrinterOrientation);
var
ADevice, ADriver, APort: array [0..255] of Char;
DeviceHandle: THandle;
DevMode: PDeviceMode; // A Pointer to a TDeviceMode structure
begin
{ First obtain a handle to the TPrinter's DeviceMode structure }
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
{ If DeviceHandle is still 0, then the driver was not loaded. Set
the printer index to force the printer driver to load making the
handle available }
if DeviceHandle = 0 then
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
end;
{ If DeviceHandle is still 0, then an error has occurred. Otherwise,
use GlobalLock() to get a pointer to the TDeviceMode structure }
if DeviceHandle = 0 then
Raise Exception.Create('Could Not Initialize TDeviceMode structure')
else
DevMode := GlobalLock(DeviceHandle);
with DevMode^ do
begin
dmFields := DM_DEFAULTSOURCE;
dmDefaultSource := ToBin;
end;
Printer.Orientation := ToOrientation;
if not DeviceHandle = 0 then
GlobalUnlock(DeviceHandle);
end;
My question is is there a better method that can set a printer tray and Printer.Orientation at the same time?