Solved

Sending data to chosen Paper Source

Posted on 1998-03-31
1
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 97
select query - oracle 16 103
How to define IfThen functions in one common unit? 4 79
Slow Restore if incremental backups using RDiff.exe 4 51
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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