Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Sending data to chosen Paper Source

I have two combo boxes that choose printer and paper source information, and store this info in the registry for my app.
How do I tell windows to use my selected paper source, the printer is easy. I don't want to use the printer setup dialog.
0
JasonC
Asked:
JasonC
1 Solution
 
d003303Commented:
Hi again,
I included the code into my code of the previuos answer. The main thing is to get a BinID-List and set the desired bin before the job starts. The Bin-ID is stored as a Word in the Data property of the Bin-Nodes, the Printer-ID as an Integer at the same place.

//////////////////////////
//
// to be unit1.pas
//
unit Unit1;

interface

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

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

var
  Form1: TForm1;

function MyDocumentProperties(hWnd: HWND; hPrinter: THandle; pDeviceName: PChar;
  const pDevModeOutput: PDeviceMode; pDevModeInput: PDeviceMode;
  fMode: DWORD): Longint; stdcall;

implementation

{$R *.DFM}

const
  winspl = 'winspool.drv';

function MyDocumentProperties; external winspl name 'DocumentPropertiesA';

procedure TForm1.Button1Click(Sender: TObject);
const cchBinName = 24;
var Index1,
    Index2,
    Needed       : Integer;
    ADeviceMode  : THandle;
    RootNode,
    PrinterNode,
    TempNode     : TTreeNode;
    ADevice,
    ADriver,
    APort        : array[0..255] of char;
    BinIDArray,
    BinNameArray : PChar;
    BinID        : Word;
    BinName      : array[0..24] of Char;
begin
  TreeView1.Items.Clear;
  RootNode := TreeView1.Items.Add(nil, 'Printers');
  for Index1 := 0 to Printer.Printers.Count - 1 do
   begin
     Printer.PrinterIndex := Index1;
     PrinterNode := TreeView1.Items.AddChild(RootNode, Printer.Printers[Index1]);
     PrinterNode.Data := Pointer(LongInt(Index1));
     Printer.GetPrinter(ADevice, ADriver, APort, ADeviceMode);
     Needed := DeviceCapabilities(ADevice, APort, dc_binnames, nil, nil);
     GetMem(BinNameArray, cchBinName * Needed);
     GetMem(BinIDArray, 2 * Needed);
     FillChar(BinNameArray[0], cchBinName * Needed, 0);
     FillChar(BinIDArray[0], 2 * Needed, 0);
     try
       DeviceCapabilities(ADevice, APort, dc_binnames, BinNameArray, nil);
       DeviceCapabilities(ADevice, APort, dc_bins, BinIDArray, nil);
       for Index2 := 1 to Needed do
        begin
          move(BinNameArray[cchBinName * (Index2 - 1)], BinName[0], cchBinName);
          move(BinIDArray[2 * (Index2 - 1)], BinID, 2);
          TempNode := TreeView1.Items.AddChild(PrinterNode, String(BinName));
          TempNode.Data := Pointer(LongInt(BinID));
        end;
     finally
       FreeMem(BinIDArray, 2 * Needed);
       FreeMem(BinNameArray, cchBinName * Needed);
     end;
   end;
  Printer.PrinterIndex := -1;
end;

procedure TForm1.Button2Click(Sender: TObject);
var ANode       : TTreeNode;
    PrinterID   : Integer;
    BinID       : Word;
    ADevice,
    ADriver,
    APort       : array[0..255] of char;
    ADeviceMode : THandle;
    DevMode     : PDeviceMode;
begin
  ANode := TreeView1.Selected;
  if ANode <> nil then
   if ANode.Parent <> nil then
    if ANode.Parent.Text <> 'Printers' then
     begin
       BinID := Word(ANode.Data);
       PrinterID := Integer(ANode.Parent.Data);

       Printer.PrinterIndex := PrinterID;
       Printer.GetPrinter(ADevice, ADriver, APort, ADeviceMode);
       DevMode := GlobalLock(ADeviceMode);
       DevMode^.dmDefaultSource := BinID;
       Printer.SetPrinter(ADevice, ADriver, APort, ADeviceMode);
       GlobalUnlock(ADeviceMode);

       Printer.Title := 'Hello World!';
       Printer.BeginDoc;
       Printer.Canvas.TextOut(200, 200, 'Hello World!');
       Printer.EndDoc;
       Printer.PrinterIndex := -1;
     end;
end;

end.

//////////////////////////
//
// to be unit1.dfm
//
object Form1: TForm1
  Left = 200
  Top = 108
  BorderStyle = bsDialog
  Caption = 'Form1'
  ClientHeight = 375
  ClientWidth = 262
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 8
    Top = 8
    Width = 245
    Height = 329
    Indent = 19
    TabOrder = 0
  end
  object Button1: TButton
    Left = 8
    Top = 344
    Width = 121
    Height = 25
    Caption = 'Query Printers'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 132
    Top = 344
    Width = 121
    Height = 25
    Caption = 'Print to selected bin'
    TabOrder = 2
    OnClick = Button2Click
  end
end

Have fun,
Slash/d003303
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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