Solved

Sending data to chosen Paper Source

Posted on 1998-03-31
1
220 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

22 Experts available now in Live!

Get 1:1 Help Now