• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2007
  • Last Modified:

TPrinter - Paper tray selection

Is there a way to select the printers paper tray unsing Delphi 3's TPrinter object?
0
Roadrunner100598
Asked:
Roadrunner100598
  • 2
  • 2
1 Solution
 
intheCommented:
here is some code to show how to change the bin/papertray and papersizes etc:

unit Unit1;

interface

uses
  Windows, Messages,Printers, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port   : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
begin
  Printer.GetPrinter(Device, Driver, Port, hDMode);
  if hDMode <> 0 then begin
    pDMode := GlobalLock(hDMode);
    if pDMode <> nil then begin

     {Set to legal}
      pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
      pDMode^.dmPaperSize := DMPAPER_LEGAL;

     {Set to custom size}
      pDMode^.dmFields := pDMode^.dmFields or
                          DM_PAPERSIZE or
                          DM_PAPERWIDTH or
                          DM_PAPERLENGTH;
      pDMode^.dmPaperSize := DMPAPER_USER;
      pDMode^.dmPaperWidth := 100 {SomeValueInTenthsOfAMillimeter};
      pDMode^.dmPaperLength := 100 {SomeValueInTenthsOfAMillimeter};

     {Set the bin to use}
      pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
      pDMode^.dmDefaultSource := DMBIN_MANUAL;

      GlobalUnlock(hDMode);
   end;
  end;
  Printer.PrinterIndex := Printer.PrinterIndex;  Printer.BeginDoc;
Printer.Canvas.Font.Name := 'Courier New';      Printer.Canvas.Font.Size := 10;
Printer.Canvas.Font.Color:=ClRed;
 Printer.Canvas.TextOut(100,100, 'Test 1');  Printer.EndDoc;
end;
end.


i advise have a look in windows.pas for more bins and paper sizes under:
{ paper selections } heading.there is quite a lot.


Regards Barry
0
 
Roadrunner100598Author Commented:
Thank you Barry, your code looks good.

But is it really neccessary to do an EndDoc - BeginDoc? I would rather change the tray during printing.

The background is: I'm printing invoices (some hundreds) with copies and was asked if I could print the copies on another tray (Original: with company logo, Copy: white paper). If I use BeginDoc - EndDoc I end up with hundereds of very small print jobs and that gives our network printer a lot of pain...
0
 
intheCommented:
hi,
that code has been used as an example by many experts here ;-)
it was actually an example of changing papersizes but i noticed it showed how to change the tray as well.
its only an example and doesnt need to be too closely followed just gives the idea.the begin/end doc is just to show when testing the code that the tray has been changed..
may be an idea to put the tray changing code into a function of its own and just call it when needed passing the tray name as a parameter or something..

0
 
Roadrunner100598Author Commented:
I see... I just didn't want to find it out the hard way.

Thanks again,

Roadrunner
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now