Solved

Sending data to chosen Paper Source

Posted on 1998-03-31
1
221 Views
Last Modified: 2010-04-06
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
Comment
Question by:JasonC
1 Comment
 
LVL 4

Accepted Solution

by:
d003303 earned 40 total points
ID: 1361140
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now