Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Duplex in delphi

Status
Not open for further replies.

msgopala

Programmer
Oct 30, 2008
43
US
Trying to access the printer settings and switch bins while printing duplex forms.

Have tried the code from here thread102-1079747. Printer does not switch bins and will not print duplex.

My code.

procedure TDecPageModule.PrintForms;
var
pDevice,pDriver,pPort : array [0..255] of Char;
S : string;
hDeviceMode : THandle;

procedure ChangePrinterBin(ToBin: Integer);
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(pDevice, pDriver, pPort, 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;

if not (DeviceHandle = 0) then
GlobalUnlock(DeviceHandle);
end;

procedure ChangePrinterDuplex(ToMode: Integer);
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(pDevice, pDriver, pPort, 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_DUPLEX;
dmDuplex := ToMode;
end;

if not (DeviceHandle = 0) then
GlobalUnlock(DeviceHandle);
end;

procedure ChangeOptions(BinNum,DuplexMode : smallint);
begin
(* First obtain a handle to the TPrinter's DeviceMode structure *)
Printer.GetPrinter(pDevice, pDriver, pPort, DeviceHandle);
ChangePrinterBin(BinNum);
ChangePrinterDuplex(DuplexMode);
end;

Need help figuring out what I'm doing wrong
 
I'm not sure if this will solve yours, but I had a similar problem in one of my projects. I'd tried several things. I went to the unit to see what I'd done and the only line now in that problem area is:
Code:
Printer.Refresh;
So, you might try:

Code:
procedure ChangeOptions(BinNum,DuplexMode : smallint);
  begin
    (* First obtain a handle to the TPrinter's DeviceMode structure *)
    Printer.Refresh;
    Printer.GetPrinter(pDevice, pDriver, pPort, DeviceHandle);
    Printer.Refresh;
    ChangePrinterBin(BinNum);
    Printer.Refresh;
    ChangePrinterDuplex(DuplexMode);
  end;
The printer object needs the refresh for it to hold (know) the current config, otherwise the "Get" returns what's in memory, not the device values. That was my conclusion anyway. HTH


Roo
Delphi Rules!
 
Roo,

Thank you so much for the reply. But Printer.Refresh after Printer.GetPrinter and before ChangePrinterBin(BinNum) will not and does not work as after the refresh the printer info held by GetPrinter is lost and so the code executes without changing the bin.

Thank you for trying.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top